美文网首页
python爬虫——urllib实战

python爬虫——urllib实战

作者: 李源源yyL | 来源:发表于2017-11-25 20:16 被阅读0次

1.urllib基础:
urlretrieve():可以一次性将某一个网页直接爬到本地
例1:

import urllib.request
web = urllib.request.urlretrieve("http://www.hellobi.com", filename="E:/1.html")
print(web)

urlcleanup():将urlretrieve()产生的缓存清掉,加快运行速度。
info():显示信息
getcode():返回状态码,如果返回200状态码,意思是正确的爬取,如果返回的是403,就是禁止爬取
geturl():查看当前爬取的网站是什么
例2:

import urllib.request
web = urllib.request.urlretrieve("http://www.hellobi.com", filename="E:/1.html")
print(web)
urllib.request.urlcleanup()
file = urllib.request.urlopen("http://www.hellobi.com")
a = file.info()
b = file.getcode()
c = file.geturl()
print(a)
print(b)
print(c)

2.超时设置:timeout 单位秒
爬虫有时候会因为爬去某些网页速度极慢,影响性能。所有可以设置超时时间。

相关文章

网友评论

      本文标题:python爬虫——urllib实战

      本文链接:https://www.haomeiwen.com/subject/whztbxtx.html