【发布时间】:2018-11-17 05:34:32
【问题描述】:
即使 Google 的官方 API 没有在查询结果中提供时间信息——即使没有对关键字进行时间过滤,高级搜索中也有时间过滤选项:
Google results for stackoverflow in the last one hour
GoogleScraper 库提供了许多灵活的选项,但与时间相关。如何使用库添加时间特征?
【问题讨论】:
标签: python web-scraping google-search
即使 Google 的官方 API 没有在查询结果中提供时间信息——即使没有对关键字进行时间过滤,高级搜索中也有时间过滤选项:
Google results for stackoverflow in the last one hour
GoogleScraper 库提供了许多灵活的选项,但与时间相关。如何使用库添加时间特征?
【问题讨论】:
标签: python web-scraping google-search
经过一番检查,我发现当时谷歌通过qdr值将过滤信息发送到tbs键(可能是指time based search虽然没有官方说明):
https://www.google.com/search?tbs=qdr:h1&q=stackoverflow
这将获取过去一小时的结果。 m 和 y 字母可以分别用于月份和年份。
另外,要添加按日期排序的功能,还需要添加 sbd(应该是 sort by date)值:
https://www.google.com/search?tbs=qdr:h1,sbd:1&q=stackoverflow
我能够将这些关键字插入到 GoogleScraper 的 BASE Google URL 中。在scraping.py 中将下面的行插入到get_base_search_url_by_search_engine() 方法的末尾(就在return 之前):
if("google" in str(specific_base_url)):
specific_base_url = "https://www.google.com/search?tbs=qdr:{},sbd:1".format(config.get("time_filter", ""))
现在在您的配置中使用time_filter 选项:
from GoogleScraper import scrape_with_config
config = {
'use_own_ip': True,
'keyword_file': "keywords.txt",
'search_engines': ['google'],
'num_pages_for_keyword': 2,
'scrape_method': 'http',
"time_filter": "d15" #up to 15 days ago
}
search = scrape_with_config(config)
结果将仅包括时间范围。此外,结果中的文本 sn-ps 将包含原始日期信息:
one_sample_result = search.serps[0].links[0]
print(one_sample_result.snippet)
4 分钟前 这一定很简单 - let propertytotalPriceOfOrder = order.items.map(item => +item.unit * +item.quantity * +item.price);. 其中 order 是您的整个 json 对象。
【讨论】: