Python 请求错误 10060

标签 python web-crawler python-requests urllib

我有一个抓取网站的脚本。
直到今天它运行完美,
然而,它现在不这样做了。

它给了我以下错误:

 Connection Aborted Error(10060 ' A connection attempt failed becvause the connected party did not properly respond after a period of time, or established a connection failed because connected host has failed to respond'

我一直在寻找答案和设置,但我不知道如何解决这个问题...

在 IE 中,我没有使用任何代理(连接 -> 局域网设置 -> 代理 = 已禁用)

它打破了这段代码,有时是第一次运行,有时是第二次......等等
def geturls(functionurl, runtime):
startCrawl = requests.get(functionurl, headers=headers)
mainHtml = BeautifulSoup(startCrawl.content, 'html.parser')
mainItems = mainHtml.find("div",{"id": "js_multiselect_results"})
for tag in mainItems.findAll('a', href=True):
    tag['href'] = urlparse.urljoin(url,tag['href'])
    if shorturl in tag['href'] and tag['href'] not in visited:
        if any(x in tag['href'] for x in keepout):
            falseurls.append(tag['href'])
        elif tag['href'] in urls:
            doubleurls.append(tag['href'])
        else:
            urlfile.write(tag['href'] + "\n")
            urls.append(tag['href'])

totalItemsStart = str(mainHtml.find("span",{"id": "sab_header_results_size"}))
if runtime == 1:
    totalnumberofitems[0] = totalItemsStart
    totalnumberofitems[0] = strip_tags(totalnumberofitems[0])
return totalnumberofitems

我怎样才能解决这个问题?

最佳答案

尝试增加 timeout您的 requests.get 的参数方法 :

requests.get(functionurl, headers=headers, timeout=5)

但是很有可能您的脚本被服务器阻止以防止报废尝试。如果是这种情况,您可以尝试通过设置适当的标题来伪造 Web 浏览器。
{"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 GTB7.1 (.NET CLR 3.5.30729)", "Referer": "http://example.com"}

关于Python 请求错误 10060,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27752644/

相关文章:

Python 队列 - task_done() 的数量

python - os.walk 不走路

python - 分隔 (X,Y) 列表

Python网络爬虫和 "getting"html源代码

python - 如何在python请求库中实现重试机制?

python - 混淆矩阵缺失实例

solr - 抓取网页后识别产品,进行价格比较

javascript - Scrapy从javascript脚本中提取数据

python - 如何通过 TOR 和 Privoxy 使用 Python 请求获取 HTTPS 内容

python - 使用 python requests 模块登录基于 WordPress 的网站