资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

在Python中使用requests库爬取数据时返回为空如何解决-创新互联

在Python中使用requests库爬取数据时返回为空如何解决?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

创新互联主营汉源网站建设的网络公司,主营网站建设方案,重庆APP开发,汉源h5成都微信小程序搭建,汉源网站营销推广欢迎汉源等地区企业咨询

Python主要用来做什么

Python主要应用于:1、Web开发;2、数据科学研究;3、网络爬虫;4、嵌入式应用开发;5、游戏开发;6、桌面应用开发。

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库爬取数据时返回为空如何解决-创新互联
网站网址:http://cdkjz.cn/article/phcci.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220