网络爬虫:从python2到python3

来源:互联网 发布:淘宝号买卖平台 编辑:程序博客网 时间:2024/04/30 06:50

很久以前,python2的时候,简单的弄过一点爬虫程序,后来,到3之后,发现之前的好多程序都特么不能用了,最最基本的抓页面都不行了,就重新写了一个。

python2缩写版,大概是这样的,忘记了没验证

import urllib2response = urllib2.urlopen('http://www.baidu.com/')html = response.read()print html

python3详细版

import urllib.requestrequest = urllib.request.Request('http://www.baidu.com/')response = urllib.request.urlopen(request)if response.getcode() != 200:    print("None!")else:    html = response.read()    # 如果返回结果不为空    if html is not None:        # 还必须编码,不然格式不对        html = html.decode("utf-8")        print(html)    else:        print("Maybe The Program is Error!")# 头信息print(response.info())
原创粉丝点击