【发布时间】: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