Python用requests库爬取返回为空的解决办法
作者:qq_38796636 时间:2021-10-30 04:54:29
首先介紹一下我們用360搜索派取城市排名前20。
我们爬取的网址:https://baike.so.com/doc/24368318-25185095.html
我们要爬取的内容:
html字段:
robots协议:
现在我们开始用python IDLE 爬取
import requests
r = requests.get("https://baike.so.com/doc/24368318-25185095.html")
r.status_code
r.text
结果分析,我们可以成功访问到该网页,但是得不到网页的结果。被360搜索识别,我们将headers修改。
输出有个小插曲,网页内容很多,我是想将前500个字符输出,第一次格式错了
import requests
headers = {
'Cookie':'OCSSID=4df0bjva6j7ejussu8al3eqo03',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
'(KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
}
r = requests.get("https://baike.so.com/doc/24368318-25185095.html", headers = headers)
r.status_code
r.text
接着我们对需要的内容进行爬取,用(.find)方法找到我们内容位置,用(.children)下行遍历的方法对内容进行爬取,用(isinstance)方法对内容进行筛选:
import requests
from bs4 import BeautifulSoup
import bs4
headers = {
'Cookie':'OCSSID=4df0bjva6j7ejussu8al3eqo03',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
'(KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
}
r = requests.get("https://baike.so.com/doc/24368318-25185095.html", headers = headers)
r.status_code
r.encoding = r.apparent_encoding
soup = BeautifulSoup(r.text, "html.parser")
for tr in soup.find('tbody').children:
if isinstance(tr, bs4.element.Tag):
tds = tr('td')
print([tds[0].string, tds[1].string, tds[2].string])
得到结果如下:
修改输出的数目,我们用Clist列表来存取所有城市的排名,将前20个输出代码如下:
import requests
from bs4 import BeautifulSoup
import bs4
Clist = list() #存所有城市的列表
headers = {
'Cookie':'OCSSID=4df0bjva6j7ejussu8al3eqo03',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
'(KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
}
r = requests.get("https://baike.so.com/doc/24368318-25185095.html", headers = headers)
r.encoding = r.apparent_encoding #将html的编码解码为utf-8格式
soup = BeautifulSoup(r.text, "html.parser") #重新排版
for tr in soup.find('tbody').children: #将tbody标签的子列全部读取
if isinstance(tr, bs4.element.Tag): #筛选tb列表,将有内容的筛选出啦
tds = tr('td')
Clist.append([tds[0].string, tds[1].string, tds[2].string])
for i in range(21):
print(Clist[i])
最终结果:
来源:https://blog.csdn.net/qq_38796636/article/details/95223267
标签:Python,requests,返回,为空
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
PyTorch CNN实战之MNIST手写数字识别示例
2021-09-06 15:20:21
![](https://img.aspxhome.com/file/2023/5/79575_0s.jpg)
简单的淡入淡出图片轮换效果
2009-05-22 18:38:00
详解Pymongo常用查询方法总结
2022-06-01 09:13:30
![](https://img.aspxhome.com/file/2023/7/105387_0s.png)
python读写修改Excel之xlrd&xlwt&xlutils
2022-04-03 16:35:43
![](https://img.aspxhome.com/file/2023/9/94149_0s.jpg)
Python使用正则表达式过滤或替换HTML标签的方法详解
2023-02-08 10:49:52
快速了解Python相对导入
2023-07-29 18:35:30
PHP下常用正则表达式整理
2023-11-18 03:04:48
Python max函数中key的用法及原理解析
2022-09-09 04:00:24
![](https://img.aspxhome.com/file/2023/7/97247_0s.png)
Python入门之字符串操作详解
2023-01-27 18:45:08
![](https://img.aspxhome.com/file/2023/5/107465_0s.png)
Python按天实现生成时间范围序列的方法详解
2022-12-31 13:48:45
ASP使用连接MYSQL数据库全攻略
2007-09-23 09:06:00
详解python上传文件和字符到PHP服务器
2023-10-15 19:02:12
jQuery 1.3的VS智能提示下载
2009-01-18 12:54:00
![](https://img.aspxhome.com/file/UploadPic/20091/18/ysc7nc_jquery_1-68s.gif)
Python3之简单搭建自带服务器的实例讲解
2022-03-05 20:50:33
![](https://img.aspxhome.com/file/2023/4/102764_0s.jpg)
Python中免验证跳转到内容页的实例代码
2021-03-27 11:19:55
![](https://img.aspxhome.com/file/2023/8/99478_0s.png)
在Laravel 中实现是否关注的示例
2023-11-14 15:22:57
详解python函数传参是传值还是传引用
2023-11-13 13:25:58
![](https://img.aspxhome.com/file/2023/4/63984_0s.jpg)
python实现二维插值的三维显示
2022-05-28 14:17:58
![](https://img.aspxhome.com/file/2023/6/121476_0s.jpg)
Golang中字符串(string)与字节数组([]byte)一行代码互转实例
2023-09-17 14:37:07
![](https://img.aspxhome.com/file/2023/8/101038_0s.png)
python实现层次聚类的方法
2023-05-03 22:26:13
![](https://img.aspxhome.com/file/2023/5/90845_0s.png)