婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > Python用requests庫爬取返回為空的解決辦法

Python用requests庫爬取返回為空的解決辦法

熱門標簽:深圳網絡外呼系統代理商 柳州正規電銷機器人收費 外呼系統前面有錄音播放嗎 千呼ai電話機器人免費 400電話辦理費用收費 鎮江人工外呼系統供應商 騰訊地圖標注有什么版本 申請辦個400電話號碼 高德地圖標注字母

首先介紹一下我們用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])

最終結果:


到此這篇關于Python用requests庫爬取返回為空的解決辦法的文章就介紹到這了,更多相關Python requests返回為空內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Python requests庫參數提交的注意事項總結
  • 詳解python requests中的post請求的參數問題
  • python requests完成接口文件上傳的案例
  • python爬取豆瓣電影排行榜(requests)的示例代碼
  • requests在python中發送請求的實例講解
  • python 實現Requests發送帶cookies的請求
  • python requests庫的使用
  • Python+unittest+requests+excel實現接口自動化測試框架
  • python爬蟲利器之requests庫的用法(超全面的爬取網頁案例)
  • python爬蟲 requests-html的使用
  • python requests模塊的使用示例

標簽:海南 哈爾濱 合肥 大慶 烏蘭察布 郴州 烏蘭察布 平頂山

巨人網絡通訊聲明:本文標題《Python用requests庫爬取返回為空的解決辦法》,本文關鍵詞  Python,用,requests,庫爬,取,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Python用requests庫爬取返回為空的解決辦法》相關的同類信息!
  • 本頁收集關于Python用requests庫爬取返回為空的解決辦法的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 大同市| 潞城市| 合作市| 腾冲县| 林芝县| 吉林市| 瑞昌市| 福建省| 高密市| 杭州市| 安泽县| 浦江县| 建水县| 武威市| 博爱县| 保定市| 农安县| 六枝特区| 炉霍县| 万盛区| 靖宇县| 蓬溪县| 常山县| 衢州市| 桃园市| 景谷| 敦化市| 罗定市| 牡丹江市| 安吉县| 三江| 太和县| 泾源县| 苏尼特左旗| 雷州市| 莒南县| 德格县| 贡嘎县| 莫力| 民权县| 滕州市|