【问题标题】:web crawling Google - getting different results网络爬取谷歌 - 得到不同的结果
【发布时间】:2016-05-13 17:28:08
【问题描述】:

我编写了以下 Python 脚本,用于在特定日期范围内抓取和抓取 Google 新闻搜索结果的标题。虽然脚本可以运行,但它显示的是最新的搜索结果,而不是列表中提到的结果。

例如该脚本不是显示 2015 年 7 月 1 日至 2015 年 7 月 7 日的结果,而是显示 2016 年 5 月(当月)的结果

import urllib.request 
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re

#get and read the URL
url = ("https://www.google.co.in/search?q=banking&num=100&safe=off&espv=2&biw=1920&bih=921&source=lnt&tbs=cdr%3A1%2Ccd_min%3A01%2F07%2F2015%2Ccd_max%3A07%2F07%2F2015&tbm=nws")
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
html = opener.open(url)
bsObj = BeautifulSoup(html.read(), "html5lib")


#extracts all the links from the given page 
itmes  = bsObj.findAll("h3")
for item in itmes:
    itemA = item.a
    theHeading = itemA.text
    print(theHeading)

有人可以指导我获得按日期排序的所需结果的正确方法吗?

提前致谢。

【问题讨论】:

    标签: python web-scraping beautifulsoup web-crawler python-requests


    【解决方案1】:

    我做了一些测试,似乎问题来自不够详细的用户代理。 尝试替换此行:

    opener.addheaders = [('User-agent', 'Mozilla/5.0')]
    

    与:

    opener.addheaders = [('User-agent', "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:36.0) Gecko/20100101 Firefox/36.0"),
    

    它对我有用。 当然这个 User-Agent 只是一个例子。

    【讨论】:

    • 感谢@Julien Salinas 的帮助。我很感激。
    【解决方案2】:

    正如Julien Salinas 所写,这是因为没有user-agent specified in your request headers

    例如,默认的 requests user-agentpython-requests,因此 Google 会阻止请求,因为它知道这是机器人而不是“真正的”用户访问,并且您收到了具有不同选择器和元素的不同 HTML,和某种错误。 User-agent 通过将此信息添加到 HTTP request headers 来伪造用户访问。

    使用requests 库将user-agent 传递给请求headers

    headers = {
        'User-agent':
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582'
    }
    response = requests.get('YOUR_URL', headers=headers)
    

    代码和example in the online IDE

    from bs4 import BeautifulSoup
    import requests, lxml
    
    headers = {
        "User-Agent":
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582"
    }
    
    params = {
        "q": "best potato recipes",
        "hl": "en",
        "gl": "us",
        "tbm": "nws",
    }
    
    html = requests.get('https://www.google.com/search', headers=headers, params=params)
    soup = BeautifulSoup(html.text, 'lxml')
    
    for result in soup.select('.WlydOe'):
        title = result.select_one('.nDgy9d').text
        link = result['href']
        print(title, link, sep='\n')
    
    ----------
    '''
    Call of Duty Vanguard (PS5) Beta Impressions – A Champion Hill To Die On
    https://wccftech.com/call-of-duty-vanguard-ps5-beta-impressions-a-champion-hill-to-die-on/
    Warzone players call for fan-favorite MW2 map to be added to Verdansk
    https://charlieintel.com/warzone-players-call-for-fan-favorite-mw2-map-to-be-added-to-verdansk/114014/
    '''
    

    或者,您可以使用来自 SerpApi 的 Google News Results API 来实现相同的目的。这是一个带有免费计划的付费 API。

    您的情况的不同之处在于您不知道为什么某些事情不起作用,绕过搜索引擎的阻止,因为它已经为最终用户完成了,您只需要遍历结构化 JSON 并获取你想要的数据。

    要集成的代码:

    import os
    from serpapi import GoogleSearch
    
    params = {
      "engine": "google",
      "q": "Call of duty 360 no scope",
      "tbm": "nws",
      "api_key": os.getenv("API_KEY"),
    }
    
    search = GoogleSearch(params)
    results = search.get_dict()
    
    for news_result in results["news_results"]:
      print(f"Title: {news_result['title']}\nLink: {news_result['link']}\n")
    
    
    ----------
    '''
    Call of Duty Vanguard (PS5) Beta Impressions – A Champion Hill To Die On
    https://wccftech.com/call-of-duty-vanguard-ps5-beta-impressions-a-champion-hill-to-die-on/
    Warzone players call for fan-favorite MW2 map to be added to Verdansk
    https://charlieintel.com/warzone-players-call-for-fan-favorite-mw2-map-to-be-added-to-verdansk/114014/
    '''
    

    P.S - 我写了一篇关于如何抓取 Google News Results 的更深入的博文。

    免责声明,我为 SerpApi 工作。

    【讨论】:

      猜你喜欢
      • 2013-10-15
      • 2020-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多