大数据的时代信息已近覆盖了社会体系中的各行各业, 大数据是指在一定时间范围内用常规软件工具进行捕捉、管理和处理的数据集合, 需要新处理模式才能具有更强的决策力、洞察发现力和流程优化能力的海量、高增长率和多样化的信息资产。 要对网页信息进行处理就是大势所趋了。
前面已近了解过了html的基本组成,现在使用Python读取一个HTML的具体信息。 Python有许多处理网页的库,没个库的功能都足够强大,简单的介绍一下处理网页的库。
- requests -对HTTP协议进行高度封装,支持非常丰富的链接访问功能。
- PySpider -一个国人编写的强大的网络爬虫系统并带有强大的WebUI。
- bs4 -beautifulsoup4库,用于解析和处理HTML和XML。
- Scrapy- 很强大的爬虫框架,用于抓取网站并从其页面中提取结构化数据。可用于从数据挖掘到监控和自动化测试的各种用途 。
- Crawley -高速爬取对应网站的内容,支持关系和非关系数据库,数据可以导出为JSON、XML等。
- Portia -可视化爬取网页内容。
- cola -分布式爬虫框架。
- newspaper -提取新闻、文章以及内容分析。
- lxml -lxml是python的一个解析库,这个库支持HTML和xml的解析,支持XPath的解析方式。
pip install requests
Requirement already satisfied: requests in /opt/conda/lib/python3.12/site-packages (2.32.3) Requirement already satisfied: charset_normalizer<4,>=2 in /opt/conda/lib/python3.12/site-packages (from requests) (3.4.1) Requirement already satisfied: idna<4,>=2.5 in /opt/conda/lib/python3.12/site-packages (from requests) (3.10) Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/conda/lib/python3.12/site-packages (from requests) (2.3.0) Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/lib/python3.12/site-packages (from requests) (2024.12.14) Note: you may need to restart the kernel to use updated packages.
在 Debian / Ubutnu 中使用:
sudo apt install -y python3-requests
完成后测试:
import requests
requests.get()
requests.get(url,params,**kwargs)
- url: 需要爬取的网站地址。
- params: url中的额外参数,字典或者字节流格式,可选。
**kwargs
: 控制访问的参数data
:字典,字节序或文件对象,重点作为向服务器提供或提交资源是提交,作为request的内容,与params不同的是,data提交的数据并不放在url链接里, 而是放在url链接对应位置的地方作为数据来存储。,它也可以接受一个字符串对象。json
:json格式的数据, json合适在相关的html,http相关的web开发中非常常见, 也是http最经常使用的数据格式, 他是作为内容部分可以向服务器提交。cookies
:字典或CookieJar,指的是从http中解析cookieauth
:元组,用来支持http认证功能,可以输入用户名,以及密码。files
:字典, 是用来向服务器传输文件时使用的字段。timeout
: 用于设定超时时间, 单位为秒,当发起一个get请求时可以设置一个timeout时间, 如果在timeout时间内请求内容没有返回, 将产生一个timeout的异常。proxies
:字典, 用来设置访问代理服务器。allow_redirects
: 开关, 表示是否允许对url进行重定向, 默认为True。stream
: 开关, 指是否对获取内容进行立即下载, 默认为True。verify
:开关, 用于认证SSL证书, 默认为True。cert
: 用于设置保存本地SSL证书路径
以下示例为向 “https://www.osgeo.cn” 对应的服务器发送相应的get请求,获得对应的内容。
url='https://www.osgeo.cn/'
html = requests.get(url, timeout=10000)
demo = html.text
html.status_code
200
requests.head(url).headers
{'Server': 'nginx/1.18.0', 'Date': 'Tue, 15 Apr 2025 05:56:58 GMT', 'Content-Type': 'text/html; charset=UTF-8', 'Connection': 'keep-alive', 'Etag': 'W/"c495dfb332011ddf4be31355d899696ef7dfae97"', 'Expires': 'Wed, 16 Apr 2025 05:56:58 GMT', 'Cache-Control': 'max-age=86400', 'Content-Encoding': 'gzip'}
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.post("http://httpbin.org/post", data=payload)
r.text
'{\n "args": {}, \n "data": "", \n "files": {}, \n "form": {\n "key1": "value1", \n "key2": "value2"\n }, \n "headers": {\n "Accept": "*/*", \n "Accept-Encoding": "gzip, deflate, br, zstd", \n "Content-Length": "23", \n "Content-Type": "application/x-www-form-urlencoded", \n "Host": "httpbin.org", \n "User-Agent": "python-requests/2.32.3", \n "X-Amzn-Trace-Id": "Root=1-67fdf52b-65568a950ccef4ee2c643956"\n }, \n "json": null, \n "origin": "47.104.10.246", \n "url": "http://httpbin.org/post"\n}\n'
r.content
b'{\n "args": {}, \n "data": "", \n "files": {}, \n "form": {\n "key1": "value1", \n "key2": "value2"\n }, \n "headers": {\n "Accept": "*/*", \n "Accept-Encoding": "gzip, deflate, br, zstd", \n "Content-Length": "23", \n "Content-Type": "application/x-www-form-urlencoded", \n "Host": "httpbin.org", \n "User-Agent": "python-requests/2.32.3", \n "X-Amzn-Trace-Id": "Root=1-67fdf52b-65568a950ccef4ee2c643956"\n }, \n "json": null, \n "origin": "47.104.10.246", \n "url": "http://httpbin.org/post"\n}\n'
r.json()
{'args': {}, 'data': '', 'files': {}, 'form': {'key1': 'value1', 'key2': 'value2'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate, br, zstd', 'Content-Length': '23', 'Content-Type': 'application/x-www-form-urlencoded', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.32.3', 'X-Amzn-Trace-Id': 'Root=1-67fdf52b-65568a950ccef4ee2c643956'}, 'json': None, 'origin': '47.104.10.246', 'url': 'http://httpbin.org/post'}
r = requests.get('https://api.github.com/events', stream=True)
r.raw
<urllib3.response.HTTPResponse at 0x7f2bbc504e20>
r.raw.read(10)
b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'