【问题标题】:Scraping Google News with pygooglenews使用 pygooglenews 抓取 Google 新闻
【发布时间】:2021-03-05 11:40:25
【问题描述】:

我正在尝试使用 pygooglenews 从 Google 新闻中进行抓取。 我试图通过使用 for 循环更改目标日期来一次抓取 100 多篇文章(因为谷歌将限制设置为 100)。以下是我到目前为止的内容,但我不断收到错误消息

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-84-4ada7169ebe7> in <module>
----> 1 df = pd.DataFrame(get_news('Banana'))
      2 writer = pd.ExcelWriter('My Result.xlsx', engine='xlsxwriter')
      3 df.to_excel(writer, sheet_name='Results', index=False)
      4 writer.save()

<ipython-input-79-c5266f97934d> in get_titles(search)
      9 
     10     for date in date_list[:-1]:
---> 11         search = gn.search(search, from_=date, to_=date_list[date_list.index(date)])
     12         newsitem = search['entries']
     13 

~\AppData\Roaming\Python\Python37\site-packages\pygooglenews\__init__.py in search(self, query, helper, when, from_, to_, proxies, scraping_bee)
    140         if from_ and not when:
    141             from_ = self.__from_to_helper(validate=from_)
--> 142             query += ' after:' + from_
    143 
    144         if to_ and not when:

TypeError: unsupported operand type(s) for +=: 'dict' and 'str'
import pandas as pd
from pygooglenews import GoogleNews
import datetime

gn = GoogleNews()

def get_news(search):
    stories = []
    start_date = datetime.date(2021,3,1)
    end_date = datetime.date(2021,3,5)
    delta = datetime.timedelta(days=1)
    date_list = pd.date_range(start_date, end_date).tolist()
    
    for date in date_list[:-1]:
        search = gn.search(search, from_=date.strftime('%Y-%m-%d'), to_=(date+delta).strftime('%Y-%m-%d'))
        newsitem = search['entries']

        for item in newsitem:
            story = {
                'title':item.title,
                'link':item.link,
                'published':item.published
            }
            stories.append(story)

    return stories

df = pd.DataFrame(get_news('Banana'))

提前谢谢你。

【问题讨论】:

  • 能否请您澄清此错误的来源,即,您能否分享完整的追溯信息?另外,您使用的是哪个pygooglenews 库?
  • 您好 p3j4p5,感谢您的回复。为缺乏解释而道歉。我已经更新了我的帖子,其中包含错误消息以及我正在使用的库的链接。
  • 谢谢@will418 - 这很有帮助,我已经写了一个答案。
  • 谢谢你,它工作得很好!我能做些什么来支持你的工作吗?
  • 不客气。如果您能接受upvote答案,那将不胜感激。

标签: python for-loop web-scraping pygooglenews


【解决方案1】:

您似乎正确地将字符串传递给get_news(),然后将其作为第一个参数 (search) 传递给gn.search()

但是,您将search 重新分配给该行中gn.search() 的结果:

  search = gn.search(search, from_=date.strftime('%Y-%m-%d'), to_=(date+delta).strftime('%Y-%m-%d'))
# ^^^^^^
# gets overwritten with the result of gn.search()

在下一次迭代中,这个重新分配的 search 被传递到它不喜欢的 gn.search()

如果您查看pygooglenews 中的代码,看起来gn.search() 正在返回dict,这可以解释错误。

要解决这个问题,只需使用不同的变量,例如:

  result = gn.search(search, from_=date.strftime('%Y-%m-%d'), to_=(date+delta).strftime('%Y-%m-%d'))
  newsitem = result['entries']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 2022-11-19
    • 2022-08-10
    相关资源
    最近更新 更多