Python 爬虫 urllib模块:get方式
2018-12-06 21:38
716 查看
本程序以爬取 百度 首页为例
格式:
导入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)
运行结果:
<!DOCTYPE html> <!--STATUS OK--> <html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=Edge"> <meta content="always" name="referrer"> <meta name="theme-color" content="#2932e1"> <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" /> <link rel="search" type="application/opensearchdescription+xml" href="/content-search.xml" title="百度搜索" /> <link rel="icon" sizes="any" mask href="//www.baidu.com/img/baidu_85beaf5496f291521eb75ba38eacbd87.svg"> <link rel="dns-prefetch" href="//s1.bdstatic.com"/> <link rel="dns-prefetch" href="//t1.baidu.com"/> <link rel="dns-prefetch" href="//t2.baidu.com"/> <link rel="dns-prefetch" href="//t3.baidu.com"/> <link rel="dns-prefetch" href="//t10.baidu.com"/> <link rel="dns-prefetch" href="//t11.baidu.com"/> <link rel="dns-prefetch" href="//t12.baidu.com"/> <link rel="dns-prefetch" href="//b1.bdstatic.com"/> <title>百度一下,你就知道</title> <style id="css_index" index="index" type="text/css">html,body{height:100%} . . . . </body> </html> ------------------------------------------------------------------ ------------------------------------------------------------------ ------------------------------------------------------------------ 200
相关文章推荐
- python爬虫中的 urllib 模块 浅析
- python爬虫之urllib模块和requests模块学习
- python爬虫学习(一)通过urllib2模块获取html,设置用户代理
- Python基础知识——urllib模块在爬虫中的应用
- Python3爬虫进阶urllib模块的使用笔记
- Python爬虫之urllib模块1
- Python中使用urllib2模块编写爬虫的简单上手示例
- urllib模块爬虫在Python2在Python3中的使用
- Python中使用urllib2模块编写爬虫的简单上手示例
- 运维学python之爬虫基础篇(二)urllib模块使用
- Python3 urllib GET方式获取数据
- 学习Python爬虫(二):urllib库之parse模块、request模块
- 03—小白学Python爬虫之urllib的基本和进阶使用及Get、Post示例
- Python 爬虫基础 - Urllib 模块(1)
- python 跨语言数据交互、json、pickle(序列化)、urllib、requests(爬虫模块)、XML。
- python编程_python通过get方式,post方式发送http请求和接收http响应_import urllib模块,import urllib2模块,import httplib模块
- python爬虫(五)_urllib2:Get请求和Post请求
- 使用Python的urllib和urllib2模块制作爬虫的实例教程
- python 爬虫入门(2) 爬虫基础知识 ; urllib 模块 ;urllib2 模块