我一直在尝试编写代码来抓取谷歌某个日期范围内的点击次数。我通过将日期插入谷歌搜索查询来完成此操作。当我复制并粘贴它生成的链接时,它给了我正确的查询,但是当代码运行它时,我不断得到没有日期范围的搜索的点击次数。我不确定我在这里做错了什么。
from bs4 import BeautifulSoup
import requests
import re
from datetime import date, timedelta
day = date.today()
friday = day - timedelta(days=day.weekday() + 3) + timedelta(days=7)
word = "debt"
for n in range(0,32,7):
date_end = friday - timedelta(days=n)
date_beg = date_end - timedelta(days=4)
link_beg = "https://www.google.com/search?q=%s&source=lnt&tbs=cdr%%3A1%%2Ccd_min%%3A" % (word)
link_date = "%s%%2F%s%%2F%s%%2Ccd_max%%3A%s%%2F%s%%2F%s&tbm=&gws_rd=ssl" % (str(date_beg.month),str(date_beg.day),str(date_beg.year),str(date_end.month),str(date_end.day),str(date_end.year))
url = link_beg + link_date
print url,
print "\t",
r = requests.get(url)
soup = BeautifulSoup(r.content)
products = soup.findAll("div", id = "resultStats")
result = str(products[0])
results = re.findall(r'\d+', result)
number = ''.join([str(i) for i in results])
print number
例如,生成的链接之一是:
Google 搜索“2015 年 3 月 9 日至 2015 年 3 月 13 日”日期范围内的“债务”
产生的点击量应为:39,700,000
但相反,它吐出:293,000,000(这只是通用搜索产生的)