Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从谷歌抓取包含日期范围的URL_Python_Web Scraping_Jupyter Notebook - Fatal编程技术网

Python 从谷歌抓取包含日期范围的URL

Python 从谷歌抓取包含日期范围的URL,python,web-scraping,jupyter-notebook,Python,Web Scraping,Jupyter Notebook,是否有可能从谷歌搜索中获得时间范围内的结果(例如从2018年2月2日到2018年3月2日),从而避免HTTPSConnectionPool错误 我目前正在使用以下代码 urls=[] count=0 q='Brexit' for url in search(q): r = requests.get(url, timeout=None) r.status_code count=count+1 urls.append(url)

是否有可能从谷歌搜索中获得时间范围内的结果(例如从2018年2月2日到2018年3月2日),从而避免HTTPSConnectionPool错误

我目前正在使用以下代码

urls=[]
count=0

q='Brexit'

for url in search(q):
        r = requests.get(url, timeout=None)
        r.status_code
        count=count+1
        urls.append(url)   
抓取URL,最好在指定的时间窗口内。但是,我无法执行此操作,因为我遇到以下错误:

HTTPSConnectionPool(host='www.\uu.\uu.org',port=443):最大重试次数 url已超出:/Brexit/(由以下原因引起) NewConnectionError(':未能建立新连接:[错误号60] 操作超时('))

我不知道我是否能修复它和/或如何修复它。

您能告诉我是否有办法避免以下错误消息并在指定的日期范围内获得结果吗?谢谢你抽出时间

使用日期概括查询。 因此,对于谷歌来说,你可以为特定的日期设置范围操作符

如果您的
search
函数只是
request.get('https://google.com/search?q={}.format(q))
您可以使用以下代码:

urls=[]
count=0

q='Brexit before:02-03-2018 after:02-02-2018'

for url in search(q):
        r = requests.get(url, timeout=None)
        r.status_code
        count=count+1
        urls.append(url)   
这将解决日期问题。对于错误,我认为您正在尝试获取一个url,其中
href
是“\uuuu”。这可能只是一个坏链接,所以我会添加一个尝试,除了块和日志。因此,我会考虑更多的过滤(使用<代码> BeautifulSoup <代码>来获得结果。 因此,代码应该是:

import request
from bs4 import BeautifulSoup

q='Brexit before:02-03-2018 after:02-02-2018'
req = request.get('https://google.com/search?q={}'.format(q), timeout=None)
soup = BeautifulSoup(req.content, 'html.parser')
hrefs = soup.find_all('a') # this is all the hrefs. 
for href in hrefs: 
     try: 
         r = requests.get(url)
     except Exception as e: 
         print(e)

你能添加更多的代码吗?搜索是使用谷歌api还是你的自定义函数?这是我目前正在使用的代码。我可以添加我正在考虑的库,但现在它返回我(或者至少它应该返回)来自谷歌的URL。我没有使用谷歌API,因为我认为你可以得到100个结果(我不确定)。与此代码相关的问题有两个:第一个是停止代码的错误;第二个是未实现的日期范围,因为我不知道如何实现。什么是
search
函数/它使用的是哪个库?因此对于特定于google的,您可以在查询本身中使用日期筛选器。因此
requests.get('https://www.google.com/search?q=brexit+在%3A02-03-2018之前+在%3A02-02-2018'之后,超时=无)
非常感谢你,grahum。我需要在一个更一般的查询中包括,比如开始日期=“…”和结束日期=“…”,但我认为这是可行的。你知道如何避免错误消息吗?如果你可以添加你的评论和其他信息(如果你知道的话)作为答案,我会标记它