繁体   English   中英

解析学校网站时出现TimeoutError:[WinError 10060]

[英]I get TimeoutError: [WinError 10060] when I parsing my school website

# -*- coding: UTF-8 -*-

import urllib.request
import re
import os

os.system("cls")

url=input("Url Link : ")

if(url[0:8]=="https://"):
   url=url[:4]+url[5:]

if(url[0:7]!="http://"):
    url="http://"+url

value=urllib.request.urlopen(url).read().decode('UTF8')
par='<title>(.+?)</title>'

result=re.findall(par,value) 
print(result)

这是标题解析程序。 在像Google,Gmail网站这样的解析时效果很好。 尝试解析我的学校网站时出现错误。 在学校有问题吗? 还是在我的代码中?

您可以增加超时

码:

value=urllib.request.urlopen(url,timeout=60).read().decode('UTF8')

使用Python请求( http://docs.python-requests.org/en/latest/ ),我可以下载http://jakjeon.icems.kr/main.do且没有错误,尽管由于无法为Windows安装朝鲜语代码页(949)。

这是脚本:

import requests

url='http://jakjeon.icems.kr/main.do'
r = requests.get(url)
print(r.status_code)
print(r.headers['content-type'])
print(r.encoding)
print(r.text)

运行它打印:

200  # r.status_code
text/html; charset=UTF-8 # r.headers['content-type']
UTF-8 # r.encoding

后跟页面的所有文本(r.text)

仅在将其代码页设置为65001(Unicode(UTF-8)后,才成功打印到Windows cmd.exe控制台,请参阅https://msdn.microsoft.com/zh-cn/library/windows/desktop/dd317756%28v = vs.85%29.aspx )。

尝试将输出重定向到文件会导致UnicodeEncodeError,因为我平台上文件的默认Windows编码是代码页1252(ANSI拉丁语1;西欧语(Windows))。 这是尝试打印到文件的错误消息:

Traceback (most recent call last):
  File "URLDownloadDemo.py", line 12, in <module>
    print(r.text)
  File "C:\Anaconda3\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 600-601: character maps to <undefined>

然后,可以在https://goo.gl/Cyav17上获得完整的cmd.exe控制台脚本,而在https://goo.gl/W4Sk9S上可以找到脚本的副本。

由于将输出重定向到文件时出现编码错误,我增强了脚本以使用UTF-8编码将其输出直接写到文件中。 这是新的脚本:

import requests

url='http://jakjeon.icems.kr/main.do'
r = requests.get(url)
print(r.status_code)
print(r.headers['content-type'])
print(r.encoding)
fout = open('URLDownloadDemo.output.txt', mode='wt', encoding='UTF-8')
fout.write(r.text)
fout.close()

运行此命令可以很好地工作(没有错误),并且输出文件中包含的韩文字母符号与网页源中的字母符号相同。

新脚本位于https://goo.gl/VJs2Na ,其输出文件位于https://goo.gl/4BKe8C

我正在尝试使用显示 URLError 的 seaborn 加载数据集: <urlopen error [winerror 10060]< div><div id="text_translate"><p> df = sns.load_dataset("提示")</p><p> 我正在尝试使用显示 URLError 的 seaborn 加载数据集:</p><p> 几秒钟后抛出此错误</p><pre>TimeoutError Traceback (most recent call last) File ~\AppData\Local\Programs\Python\Python311\Lib\urllib\request.py:1348, in AbstractHTTPHandler.do_open(self, http_class, req, **http_conn_args) 1347 try: -> 1348 h.request(req.get_method(), req.selector, req.data, headers, 1349 encode_chunked=req.has_header('Transfer-encoding')) 1350 except OSError as err: # timeout error File ~\AppData\Local\Programs\Python\Python311\Lib\http\client.py:1282, in HTTPConnection.request(self, method, url, body, headers, encode_chunked) 1281 """Send a complete request to the server.""" -> 1282 self._send_request(method, url, body, headers, encode_chunked) File ~\AppData\Local\Programs\Python\Python311\Lib\urllib\request.py:241, in urlretrieve(url, filename, reporthook, data) 224 """ 225 Retrieve a URL into a temporary location on disk. 226 (...) 237 data file as well as the resulting HTTPMessage object. 238 """ 239 url_type, path = _splittype(url) --> 241 with contextlib.closing(urlopen(url, data)) as fp: 242 headers = fp.info() 244 # Just return the local path and the "headers" for file:// 245 # URLs. No sense in performing a copy unless requested. URLError: <urlopen error [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond></pre><p> 我尝试更改 inte.net 连接,还尝试取消选中 LAN 设置中的代理服务器</p></div></urlopen>

[英]I am trying to load dataset using seaborn showing URLError: <urlopen error [WinError 10060]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 超时错误:[WinError 10060]: TimeoutError: [WinError 10060] 连接尝试失败 Python 套接字在其他计算机上超时。 超时错误:[WinError 10060] 为什么通过 smtplib 发送 email 时出现超时 [WinError 10060] 错误? TimeoutError:[WinError 10060] 连接尝试失败:除了块也不执行 我正在尝试使用显示 URLError 的 seaborn 加载数据集: <urlopen error [winerror 10060]< div><div id="text_translate"><p> df = sns.load_dataset("提示")</p><p> 我正在尝试使用显示 URLError 的 seaborn 加载数据集:</p><p> 几秒钟后抛出此错误</p><pre>TimeoutError Traceback (most recent call last) File ~\AppData\Local\Programs\Python\Python311\Lib\urllib\request.py:1348, in AbstractHTTPHandler.do_open(self, http_class, req, **http_conn_args) 1347 try: -> 1348 h.request(req.get_method(), req.selector, req.data, headers, 1349 encode_chunked=req.has_header('Transfer-encoding')) 1350 except OSError as err: # timeout error File ~\AppData\Local\Programs\Python\Python311\Lib\http\client.py:1282, in HTTPConnection.request(self, method, url, body, headers, encode_chunked) 1281 """Send a complete request to the server.""" -> 1282 self._send_request(method, url, body, headers, encode_chunked) File ~\AppData\Local\Programs\Python\Python311\Lib\urllib\request.py:241, in urlretrieve(url, filename, reporthook, data) 224 """ 225 Retrieve a URL into a temporary location on disk. 226 (...) 237 data file as well as the resulting HTTPMessage object. 238 """ 239 url_type, path = _splittype(url) --> 241 with contextlib.closing(urlopen(url, data)) as fp: 242 headers = fp.info() 244 # Just return the local path and the "headers" for file:// 245 # URLs. No sense in performing a copy unless requested. URLError: <urlopen error [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond></pre><p> 我尝试更改 inte.net 连接,还尝试取消选中 LAN 设置中的代理服务器</p></div></urlopen> 在 python 中使用响应时,我无法从网站获得任何响应。 我收到超时错误 10060 Python ftplib WinError 10060 无法登录我的学校网站 使用 Python 3 通过 SMTP 发送邮件时出现 TimeoutError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM