【问题标题】:Python Website Scraper - Returning Google PagePython 网站爬虫 - 返回 Google 页面
【发布时间】:2018-01-06 16:45:34
【问题描述】:

我正在尝试构建我的第一个网站抓取工具,并且对 Python 和一般编程非常陌生。我正在尝试练习抓取网站,但由于某种原因我的代码不起作用。请参阅下面的代码。当我运行代码时,它返回 google.com 的 html 而不是 County Assessors 页面。

这是我的 Python 代码的问题,还是 County Assessors 页面上的某些代码将我重新路由到谷歌?我该如何解决这个问题?任何帮助深表感谢。谢谢。

#IMPORT LIBRARIES
from urllib.request import urlopen
from bs4 import BeautifulSoup
import requests

#SCRAPER CODE
web_page = 'https://mcassessor.maricopa.gov/index.php'
page = urlopen(web_page)
soup = BeautifulSoup(page,'html.parser')
print (soup)

【问题讨论】:

    标签: python html web-scraping python-requests urllib3


    【解决方案1】:

    您需要通过这个User-Agent 标头检查:

    from bs4 import BeautifulSoup
    import requests
    
    
    web_page = 'https://mcassessor.maricopa.gov/index.php'
    response = requests.get(web_page, headers={
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
    })
    soup = BeautifulSoup(response.content, 'html.parser')
    print (soup.prettify())
    

    【讨论】:

    • 完美!感谢您的帮助。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多