Python 使用PyTrends和#x27发布刮取数据;请求失败:谷歌返回了一个代码为500的响应;

Python 使用PyTrends和#x27发布刮取数据;请求失败:谷歌返回了一个代码为500的响应;,python,web-scraping,Python,Web Scraping,我试图从今天(2019-12-09到2020-03-18)抓取365天的数据点。 我似乎无法从2020-02-27到2020-03-05之间的某个地方获取数据。我尝试了不同的关键词,同样的事情 我尝试将查询分为两部分: 一个从2019-12-09到2020-02-27,以及 第二次从2020-02-27到2020-03-18 这成功地运行了查询(无代码500),但返回了问题时间范围(2020-02-27至2020-03-05)的空数据帧 最糟糕的是,当我在上周五(2020-03-13)刮了365

我试图从今天(2019-12-09到2020-03-18)抓取365天的数据点。 我似乎无法从2020-02-27到2020-03-05之间的某个地方获取数据。我尝试了不同的关键词,同样的事情

我尝试将查询分为两部分: 一个从2019-12-09到2020-02-27,以及 第二次从2020-02-27到2020-03-18

这成功地运行了查询(无代码500),但返回了问题时间范围(2020-02-27至2020-03-05)的空数据帧

最糟糕的是,当我在上周五(2020-03-13)刮了365天(包括有问题的数据范围)后,它工作得很好-没有空的数据框。我有最新的pandas和pytrends

PS:如果你们愿意的话,我可以附加整个python文件,只是觉得它可能是不必要的

请帮忙。我不知道这是怎么发生的以及为什么发生的。我的代码如下:

def get_trends(keyword, days):
i = 0
waiting_time = 60

"""Specify start and end date as well es the required keyword for your query"""
print("Trend keyword: ", type(keyword))

"""Calculating start date from end date, based on given interval"""
end_date = datetime.datetime.date(datetime.datetime.now())
# end_date = end_date - datetime.timedelta(days=20)
print("Today's date: ", end_date)
start_date = end_date - datetime.timedelta(days=days)
print(start_date, end_date)

"""Since we want weekly data for our query, we will create lists which include
the weekly start and end date."""

weekly_date_list = []

# Adds the start date as first entry in our weekly_date_list
start_date_temp = start_date
weekly_date_list.append(start_date_temp)

# This will return in list of weekly datetime.date objects - except the end date
while start_date_temp + datetime.timedelta(days=7) <= end_date:
    start_date_temp += datetime.timedelta(days=7)
    weekly_date_list.append(start_date_temp)

# This will add the end date to the weekly_date list. We now have a complete list in the specified time frame
if start_date_temp + datetime.timedelta(days=7) > end_date:
    weekly_date_list.append(end_date)

print(weekly_date_list)

"""Now we can start to downloading the data via Google Trends API
therefore we have to specify a key which includes the start date
and the end-date with T00 as string for hourly data request"""

"""This list will contain pandas Dataframes of weekly data with the features "date",
"keyword"(which contains weekly scaling between 0 and 100), "isPartial".
Up to this point, the scaling is not correct."""

interest_list = []

# Here we download the data and print the current status of the process
while i < len(weekly_date_list) - 1:
    key = str(weekly_date_list[i]) + "T00 " + str(weekly_date_list[i+1]) + "T00"
    p = TrendReq()
    p.build_payload(kw_list=[keyword], timeframe=key)
    interest = p.interest_over_time()
    interest_list.append(interest)
    print("GoogleTrends Call {} of {} : Timeframe: {} ".format(i + 1, len(weekly_date_list) - 1, key))
    i += 1

# print(interest_list)
def get_趋势(关键字,天数):
i=0
等待时间=60
“”“指定开始和结束日期以及查询所需的关键字”“”
打印(“趋势关键字:”,类型(关键字))
“”“根据给定的间隔从结束日期计算开始日期”“”
end_date=datetime.datetime.date(datetime.datetime.now())
#结束日期=结束日期-datetime.timedelta(天数=20)
打印(“今天的日期:”,结束日期)
开始日期=结束日期-datetime.timedelta(天=天)
打印(开始日期、结束日期)
“”“由于我们需要查询的每周数据,我们将创建列表,其中包括
每周开始和结束日期。”“”
每周\日期\列表=[]
#将开始日期作为第一个条目添加到每周日期列表中
开始日期临时=开始日期
每周日期列表。附加(开始日期临时)
#这将在每周datetime.date对象列表中返回-结束日期除外
开始日期临时+日期时间。时间增量(天=7)结束日期:
每周日期列表。追加(结束日期)
打印(每周日期列表)
“现在我们可以开始通过Google Trends API下载数据了
因此,我们必须指定一个包含开始日期的键
以及以T00作为每小时数据请求字符串的结束日期“”
“”“此列表将包含具有“日期”功能的每周数据的数据帧,”,
“关键字”(包含0到100之间的每周缩放),“isPartial”。
到目前为止,缩放不正确。”“”
利息清单=[]
#在这里,我们下载数据并打印流程的当前状态
而i
我建议在面临类似问题时提交问题,但仍无补救措施!