美文网首页
Python-urllib2下载网页

Python-urllib2下载网页

作者: 冰三尺 | 来源:发表于2017-06-15 23:10 被阅读343次

方法一

import urllib2
#直接下载
url = "http://www.baidu.com"
response = urllib2.urlopen(url)
#获取状态码, 200表示成功
if response.getcode() == 200:
    #读取内容
    content = response.read()
    print content;

方法二

import urllib2
url = "http://www.baidu.com"
request = urllib2.Request(url)
request.add_header("user-agent", "Mozilla/5.0")
response1 = urllib2.urlopen(request)
if response1.getcode() == 200:
    print response1.read()

方法三

import urllib2
import cookielib
url = "http://www.baidu.com"
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
response3 = urllib2.urlopen(url)
if response3.getcode() == 200:
    print response3.read()
    print cj

相关文章

网友评论

      本文标题:Python-urllib2下载网页

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