第14.9节 Python中使用urllib.request+BeautifulSoup获取url访问的基本信息
Python 获取 信息 访问 基本 url request urllib
2023-09-27 14:26:58 时间
利用urllib.request读取url文档的内容并使用BeautifulSoup解析后,可以通过一些基本的BeautifulSoup对象输出html文档的基本信息。以博文《第14.6节 使用Python urllib.request模拟浏览器访问网页的实现代码》访问为例,读取和解析代码如下:
>>> from bs4 import BeautifulSoup
>>> import urllib.request
>>> def getURLinf(url):
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36'}
req = urllib.request.Request(url=url,headers=header)
resp = urllib.request.urlopen(req,timeout=5)
html = resp.read().decode()
soup = BeautifulSoup(html,'lxml')
return (soup,req,resp)
>>> soup,req ,resp = getURLinf(r'https://blog.csdn.net/LaoYuanPython/article/details/100629947')
可获取的基本信息包括:
1、 文档标题
>>> soup.title
<title>第14.6节 使用Python urllib.request模拟浏览器访问网页的实现代码 - 老猿Python - CSDN博客</title>
2、 文档是否为xml文档
>>> soup.is_xml
False
3、 文档的url地址
>>> req.full_url
'https://blog.csdn.net/LaoYuanPython/article/details/100629947'
>>> resp.geturl()
'https://blog.csdn.net/LaoYuanPython/article/details/100629947'
>>> resp.url
'https://blog.csdn.net/LaoYuanPython/article/details/100629947'
>>>
4、 文档所在的主机
>>> req.host
'blog.csdn.net'
5、 请求头的信息
>>> req.header_items()
[('Host', 'blog.csdn.net'), ('User-agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36')]
>>>
6、 响应状态码
>>> resp.getcode()
200
>>>
7、 响应http报文头信息
>>> resp.headers.items()
[('Date', 'Sun, 08 Sep 2019 15:07:12 GMT'), ('Content-Type', 'text/html; charset=UTF-8'), ('Transfer-Encoding', 'chunked'), ('Connection', 'close'), ('Set-Cookie', 'acw_tc=2760828215679552322374611eb7315abdcfe4ee6f7af5d157db5621c4267d;path=/;HttpOnly;Max-Age=2678401'), ('Server', 'openresty'), ('Vary', 'Accept-Encoding'), ('Set-Cookie', 'uuid_tt_dd=10_19729129290-1567955232238-614052; Expires=Thu, 01 Jan 2025 00:00:00 GMT; Path=/; Domain=.csdn.net;'), ('Set-Cookie', 'dc_session_id=10_1567955232238.557324; Expires=Thu, 01 Jan 2025 00:00:00 GMT; Path=/; Domain=.csdn.net;'), ('Vary', 'Accept-Encoding'), ('Strict-Transport-Security', 'max-age=86400')]
>>>
本节介绍了使用urllib.request读取url文档的内容并使用BeautifulSoup解析后可以很方便的获取的一些url访问的基本信息,通过这些信息可以对本次访问提供一些概要的信息。
老猿Python,跟老猿学Python!
博客地址:https://blog.csdn.net/LaoYuanPython
老猿Python博客文章目录:https://blog.csdn.net/LaoYuanPython/article/details/98245036
请大家多多支持,点赞、评论和加关注!谢谢!
相关文章
- 一个Python可视化模块,简单又好用
- 从0到1学Python丨图像平滑方法的两种非线性滤波:中值滤波、双边滤波
- Python常用函数:获取当前项目路径【abs_path=pathlib.Path(__file__).absolute()】-->【sys.path.append(str(abs_path))】
- python写入数据到clickhouse
- python+selenium自动化可视区域大小获取
- Python量化-如何获取实时股票信息
- python mysql
- [Python-jenkins]获取job构建信息
- python文件之间变量和函数的 获取/调用 的方法
- 从零起步(无需数学和Python基础)编码实现AI框架之第六节课:使用Matrix编写AI框架实战及测试
- 第15.33节 PyQt(Python+Qt)入门学习:containers容器类部件QTabWidget选项窗部件简介
- PyQt(Python+Qt)学习随笔:QListWidget获取当前选中项的selectedItems方法
- 第14.5节 利用浏览器获取的http信息构造Python网页访问的http请求头
- 第2.2节 Python的语句
- python获取的信息列表微信公共平台和用户头像
- Python 获取文件夹下指定文件后缀数量
- Python 线程,with的作用(自动获取和释放锁Lock)
- Python通过字典(dict)中value获取前n个最大的元素方法及示例代码
- Python股票历史涨跌幅数据获取
- 这个帖子要收藏,以后用得着--python 实时获取子进程输出
- python爬虫:获取电影信息~
- python ,ffprobe获取音频文件头信息,文件格式等
- python 获取上周第一天和最后一天的日期
- python - 获取时间戳(10位和13位)