Python网络数据采集
来源:互联网 发布:网络存储服务器价格 编辑:程序博客网 时间:2024/05/20 06:31
不管是使用urllib.request.urlopen()方法,还是使用opener.open()方法,都返回同样类型的HTTPResponse对象,用法总结如下:
# !/usr/bin/env python# -*- coding:utf-8 -*-from urllib import requestfrom urllib import responseURL="http://www.baidu.com/"# 构造请求头信息# 反反爬虫:设置User-Agentrequest_headers={ "User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36"}# 构造请求对象,并添加请求头req=request.Request(URL,headers=request_headers)# 发起请求resp=request.urlopen(req)print(type(resp)) # <class 'http.client.HTTPResponse'># 获取HTTP协议版本号(10 for HTTP/1.0, 11 for HTTP/1.1)print(resp.version)# 获取响应码print(resp.status)print(resp.getcode())# 获取响应描述字符串print(resp.reason)# 获取实际请求的页面url(防止重定向用)print(resp.geturl())# 获取特定响应头信息print(resp.getheader(name="Content-Type"))# 获取响应头信息,返回二元元组列表print(resp.getheaders())# 获取响应头信息,返回字符串print(resp.info())# 读取响应体print(resp.readline().decode('utf-8'))print(resp.read().decode('utf-8'))
阅读全文
0 0
- python网络数据采集
- Python网络数据采集
- Python网络数据采集
- Python网络数据采集
- Python网络数据采集
- Python网络数据采集
- Python网络数据采集
- Python网络数据采集
- Python网络数据采集
- Python网络数据采集
- Python网络数据采集
- Python网络数据采集(1)
- Python网络数据采集-BeautifulSoup
- Python网络数据采集pdf
- Python网络数据采集错误
- Python网络数据采集 中文版
- Python网络数据采集学习笔记1
- Python网络数据采集1(译者:哈雷)
- 部分项目经常会出现一些无法找到原因的js报错,如果实在不能解决,可以尝试屏蔽报错,但是不是所以情况都适用。
- Python每日小结(十)
- 机房-结账
- java面试整理
- TCP与UDP的区别总结
- Python网络数据采集
- pytorch中的pre-train函数模型引用及修改(增减网络层,修改某层参数等)
- 提交表单 之 提示文字在输入框内部,提示文字怎么处理?
- 剑指offer — 调整数组顺序使奇数位于偶数前面
- 机器学习笔记-Radial Basis Function Network
- 2182: [Spoj1479]The GbAaY Kingdom最小直径生成树
- MySQL事物和游标示例
- js滑动进度
- 关于Android studio的模拟器无法打开问题