【问题标题】:Python3 and BeautifulSoup to scrape addresses from google returns "[]" rather than the addressPython3 和 BeautifulSoup 从 google 抓取地址返回“[]”而不是地址
【发布时间】:2018-07-09 04:02:13
【问题描述】:

这是我的 python3 脚本:

    import bs4, requests
    def getCompAddress(https://www.google.com/search):
    res = requests.get(https://www.google.com/search)
    res.raise_for_status()

    soup = bs4.BeautifulSoup(res.text, 'html.parser')
    elems = soup.select('#rhs_block.LrzXr')
    print(elems)

...getCompAddress 的网址只是 google 的搜索结果。所以对于Crossings at Five Forks,google 会在右侧显示地址和电话信息。我只想抓取地址,我已将其 CSS 路径复制到 soup.select

但是当我运行这个脚本时,打印出来的只是“[]”……而不是地址。我在这里想念什么?谷歌会阻止这种抓取吗?感谢您的帮助!

【问题讨论】:

    标签: python css python-3.x web-scraping beautifulsoup


    【解决方案1】:

    您应该检查 res.text 变量以获取站点的 html 响应。

    您通常应该使用这种方式来调试 BeautifulSoup 抓取工具,因为由于许多不同的原因(例如 javascript 支持或验证码检查),结果可能与预期的不同

    另外,据我所知,谷歌有一个验证码检查来防止机器人(并让人们使用他们的付费 API)

    【讨论】:

      【解决方案2】:

      尝试使用 VPN 来获取结果。我在 github 上遇到了这个项目,它允许您通过在 Docker 容器内托管 VPN 来做到这一点https://github.com/pry0cc/ProxyDock

      Google 确实阻止了搜索结果的收集。他们阻止 Tor 和公开可用的 VPN 代理。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-19
        • 2020-05-12
        • 2011-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多