首先,我的问题与此问题非常相似.我希望urllib.urlopen()超时以生成我可以处理的异常.
这不属于URLError吗?
try:
response = urllib.request.urlopen(url, timeout=10).read().decode('utf-8')
except (HTTPError, URLError) as error:
logging.error(
'Data of %s not retrieved because %s\nURL: %s', name, error, url)
else:
logging.info('Access successful.')
Run Code Online (Sandbox Code Playgroud)
错误消息:
resp = urllib.request.urlopen(req,timeout = 10).read().decode('utf-8')
File"/usr/lib/python3.2/urllib/request.py",第138行,在urlopen中
return opener.open(url,data,timeout)
文件"/usr/lib/python3.2/urllib/request.py",第369行,打开
响应= self._open(req,data)
文件"/ usr/lib /python3.2/urllib/request.py",第387 行,在_open'_open
',req中)
文件"/usr/lib/python3.2/urllib/request.py",第347行,在_call_chain
result = func(*args)
文件"/usr/lib/python3.2/urllib/request.py",第1156行,在http_open中
返回self.do_open(http.client.HTTPConnection,req)
文件"/usr/lib/python3.2/ urllib/request.py",第1141行,在
do_open 中r = h.getresponse()
文件"/usr/lib/python3.2/http/client.py",第1046行,在getresponse
response.begin()
文件中"/ usr/lib/python3.2/http/client.py",第346行,在开始
版本,状态,原因= self._read_status()
文件"/usr/lib/python3.2/http/client.py",行308,在_read_status
line = str(self.fp.readline(_MAXLINE + 1),"iso-8859-1")
文件"/usr/lib/python3.2/socket.py ",第276行,在readinto中
返回self._sock.recv_into(b)socket.timeout
:超时
当Python 3重新组织urllib和urllib2模块时,Python …
我正在尝试使用 Python 抓取网站。我导入并使用 urllib2、BeautifulSoup 和 re 模块。
response = urllib2.urlopen(url)
soup = BeautifulSoup(response)
responseString = str(soup)
coarseExpression = re.compile('<div class="sodatext">[\n]*.*[\n]*</div>')
coarseResult = coarseExpression.findall(responseString)
fineExpression = re.compile('<[^>]*>')
fineResult = []
for coarse in coarseResult:
fine = fineExpression.sub('', coarse)
#print(fine)
fineResult.append(fine)
Run Code Online (Sandbox Code Playgroud)
不幸的是,像撇号这样的字符以损坏的方式出现,就像这样 - ' ; 有办法避免这种情况吗?或者有什么方法可以轻松替换它们?
我编写的代码生成了以下视频的链接.获得后,我尝试以这种方式下载它:
import urllib.request
import os
url = 'http://www.videodetective.net/flash/players/?customerid=300120&playerid=351&publishedid=319113&playlistid=0&videokbrate=750&sub=RTO&pversion=5.2%22%20width=%22670%22%20height=%22360%22'
response = urllib.request.urlopen(url).read()
outpath = os.path.join(os.getcwd(), 'video.mp4')
videofile = open(outpath , 'wb')
videofile.write(response)
videofile.close()
Run Code Online (Sandbox Code Playgroud)
我得到的只是该目录中一个无法读取的58kB文件.有人能指出我正确的方向吗?