Python爬虫urllib模块:get方式-创新互联
本程序以爬取 百度 首页为例
格式:
导入urllib.request
打开爬取的网页: response = urllib.request.urlopen('网址')
读取网页代码: html = response.read()
打印:
1.不decode
print(html) #爬取的网页代码会不分行,没有空格显示,很难看
2.decode
print(html.decode()) #爬取的网页代码会分行,像写规范的代码一样,看起来很舒服
查询请求结果:
a. response.status # 返回 200:请求成功 404:网页找不到,请求失败
b. response.getcode() # 返回 200:请求成功 404:网页找不到,请求失败
1.不decode的程序如下:
import urllib.request response = urllib.request.urlopen('www.baidu.com') html = response.read() print(html) print("------------------------------------------------------------------") print("------------------------------------------------------------------") print(response.status)
运行结果:
2.decode的程序如下:
import urllib.request response = urllib.request.urlopen('www.baidu.com') html = response.read() print(html.decode()) print("------------------------------------------------------------------") print("------------------------------------------------------------------") print(response.status)
运行结果:
百度一下,你就知道
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网页题目:Python爬虫urllib模块:get方式-创新互联
网站URL:http://scjiangan.com/article/edidd.html