python爬虫入门---第一篇:获取某一网站所有超链接
|
需要先安装requests库和bs4库 bs4 <span style="color: #0000ff">def<span style="color: #000000"> getHTMLText(url):<span style="color: #0000ff">try<span style="color: #000000">: <span style="color: #008000">#<span style="color: #008000">获取服务器的响应内容,并设置最大请求时间为6秒 res = requests.get(url,timeout = 6<span style="color: #000000">) <span style="color: #008000">#<span style="color: #008000">判断返回状态码是否为200 <span style="color: #000000"> res.raise_for_status() <span style="color: #008000">#<span style="color: #008000">设置真正的编码 res.encoding =<span style="color: #000000"> res.apparent_encoding <span style="color: #008000">#<span style="color: #008000">返回网页HTML代码 <span style="color: #0000ff">return<span style="color: #000000"> res.text <span style="color: #0000ff">except<span style="color: #000000">: <span style="color: #0000ff">return <span style="color: #800000">'<span style="color: #800000">产生异常<span style="color: #800000">' <span style="color: #008000">#<span style="color: #008000">目标网页 demo =<span style="color: #000000"> getHTMLText(url) <span style="color: #008000">#<span style="color: #008000">解析HTML代码 <span style="color: #008000">#<span style="color: #008000">模糊搜索HTML代码的所有标签 <span style="color: #008000">#<span style="color: #008000">获取所有标签中的href对应的值,即超链接 (编辑:长春站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- python – 在大熊猫DataFrame中选择和删除具有相同名称的列
- python – igraph:为什么add_edge函数如此缓慢地对add_edg
- python包命名空间的背后是什么:z3c,zc,collective?
- 【Python】Python多进程库multiprocessing中进程池Pool的返
- 如何避免在python中阻塞代码?
- python – 如何访问List元素
- import pyttsx在python 2.7中工作,但不在python3中
- python – 从文本文件中读取多个数字
- 使用OpenCV Python Mac编写视频
- python – Keras LSTM输入维度设置
