Python用requests库爬取返回为空的解决办法


Posted in Python onFebruary 21, 2021

首先介?一下我??用360搜索派取城市排名前20。
我们爬取的网址:https://baike.so.com/doc/24368318-25185095.html

我们要爬取的内容:

Python用requests库爬取返回为空的解决办法

html字段:

Python用requests库爬取返回为空的解决办法

robots协议:

Python用requests库爬取返回为空的解决办法

现在我们开始用python IDLE 爬取

Python用requests库爬取返回为空的解决办法

import requests
r = requests.get("https://baike.so.com/doc/24368318-25185095.html")
r.status_code
r.text

结果分析,我们可以成功访问到该网页,但是得不到网页的结果。被360搜索识别,我们将headers修改。

Python用requests库爬取返回为空的解决办法

输出有个小插曲,网页内容很多,我是想将前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])

得到结果如下:

Python用requests库爬取返回为空的解决办法

修改输出的数目,我们用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])

最终结果:

Python用requests库爬取返回为空的解决办法

到此这篇关于Python用requests库爬取返回为空的解决办法的文章就介绍到这了,更多相关Python requests返回为空内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python中使用中文的方法
Feb 19 Python
python中的列表推导浅析
Apr 26 Python
pandas 实现将重复表格去重,并重新转换为表格的方法
Apr 18 Python
python整合ffmpeg实现视频文件的批量转换
May 31 Python
python之拟合的实现
Jul 19 Python
Python中PyQt5/PySide2的按钮控件使用实例
Aug 17 Python
详解Python3 pandas.merge用法
Sep 05 Python
聊聊python中的循环遍历
Sep 07 Python
Pycharm plot独立窗口显示的操作
Dec 11 Python
浅析python实现动态规划背包问题
Dec 31 Python
pyqt5打包成exe可执行文件的方法
May 14 Python
Python中的 Set 与 dict
Mar 13 Python
python利用proxybroker构建爬虫免费IP代理池的实现
Feb 21 #Python
python实现图片转字符画的完整代码
Feb 21 #Python
利用Python实现最小二乘法与梯度下降算法
Feb 21 #Python
Scrapy实现模拟登录的示例代码
Feb 21 #Python
scrapy-splash简单使用详解
Feb 21 #Python
详解使用scrapy进行模拟登陆三种方式
Feb 21 #Python
利用Python如何画一颗心、小人发射爱心
Feb 21 #Python
You might like
可快速识别放射性物质-国外大神教你diy一个开放式辐射探测器
2020/03/12 无线电
PHP输出当前进程所有变量/常量/模块/函数/类的示例
2013/11/07 PHP
php使用Jpgraph绘制简单X-Y坐标图的方法
2015/06/10 PHP
php获取文件名称和扩展名的方法
2017/02/07 PHP
php数据结构之顺序链表与链式线性表示例
2018/01/22 PHP
PhpStorm本地断点调试的方法步骤
2018/05/21 PHP
JQuery中的$.getJSON 使用说明
2011/03/10 Javascript
浅析js封装和作用域
2013/07/09 Javascript
javascript中数组中求最大值示例代码
2013/12/18 Javascript
jquery.form.js用法之清空form的方法
2014/03/07 Javascript
轻松创建nodejs服务器(10):处理POST请求
2014/12/18 NodeJs
javascript中日期函数new Date()的浏览器兼容性问题
2015/09/05 Javascript
JavaScript中的this到底是什么(一)
2015/12/09 Javascript
有关jquery与DOM节点操作方法和属性记录
2016/04/15 Javascript
Bootstrap表单Form全面解析
2016/06/13 Javascript
jQuery购物网页经典制作案例
2016/08/19 Javascript
详解前端自动化工具gulp自动添加版本号
2016/12/20 Javascript
layui从数据库中获取复选框的值并默认选中方法
2018/08/15 Javascript
解决vuejs 使用value in list 循环遍历数组出现警告的问题
2018/09/26 Javascript
JavaScript数组去重的几种方法
2019/04/07 Javascript
基于jquery ajax的多文件上传进度条过程解析
2019/09/11 jQuery
[02:54]DOTA2亚洲邀请赛 VG战队出场宣传片
2015/02/07 DOTA
Python cookbook(数据结构与算法)让字典保持有序的方法
2018/02/18 Python
Python3.6.2调用ffmpeg的方法
2019/01/10 Python
python pytest进阶之conftest.py详解
2019/06/27 Python
Python使用Pandas库实现MySQL数据库的读写
2019/07/06 Python
纯css3实现效果超级炫的checkbox复选框和radio单选框
2014/09/01 HTML / CSS
VSCode 自定义html5模板的实现
2019/12/05 HTML / CSS
西班牙英格列斯百货法国官网:El Corte Inglés法国
2017/07/09 全球购物
护理专业自荐信范文
2014/02/26 职场文书
幼儿园教师岗位职责
2014/03/17 职场文书
社区综治工作汇报
2014/10/27 职场文书
物业保洁员岗位职责
2015/02/13 职场文书
小学班主任个人总结
2015/03/03 职场文书
2016新教师培训心得体会范文
2016/01/08 职场文书
台积电称即便经济低迷也没有降价的计划
2022/04/21 数码科技