Python实训day06am【网络爬虫(爬取接口)】
- Python實訓-15天-博客匯總表
目錄
1、“from bs4 import BeautifulSoup”解析
2、字體反爬蟲
3、網絡爬蟲
3.1、直接爬取頁面html-爬取每個章節的內容
3.2、爬取數據接口-英雄之刃頭像
3.3、爬取數據接口-課堂練習-王者頭像
1、“from bs4 import BeautifulSoup”解析
知乎:https://www.zhihu.com/question/308106778?sort=created
2、字體反爬蟲
遇到問題之后:
昨天內容:得票數,字體庫來加密展現 ,無法爬取。
? ? 𢏇 𦓛.....?
? ? 140231 --解密--> 0~9
? ? 156891 --解密--> 0~9
3、網絡爬蟲
1、直接爬取頁面html,從html中就能獲取到結果(30%)
2、爬取的html頁面只是一個空殼子,里面沒有想要的內容。(50%)
? ? 頁面中我們看到的內容,是“前端頁面通過請求后臺接口(ajax請求),得到數據”后,又充填到html中的。
? ? 重點:通過工具,找到提供數據的那個后臺接口。
3.1、直接爬取頁面html-爬取每個章節的內容
昨天的內容:爬取每個章節的內容。
import requests from bs4 import BeautifulSoup as BS# 偽裝成瀏覽器 hds = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36'}# 小說首頁路徑 url = 'https://book.qidian.com/info/1027669580/#Catalog';resp = requests.get(url, headers=hds);ct = resp.content.decode('utf-8');# print(ct) # print(ct.index('李家的劍')) # 找到了,說明爬取到的html中包含我們想要的內容;使用BeautifulSoup解析即可bs = BS(ct, 'lxml'); sa = bs.select('.volume-wrap .volume:first-child .book_name a'); #:nth-child(1)zjinfo = {}; # 名稱:url地址for a in sa:zjinfo[a.get_text()] = 'https:' + a['href'];# print(zjinfo)# 遍歷每個章節for k, v in zjinfo.items():u2 = v;resp = requests.get(u2, headers=hds);ct = resp.content.decode('utf-8');# print(ct.index('純凈的笑容')) # 可以使用BS解析,獲取想要的內容# print(ct)bs = BS(ct, 'lxml');sps = bs.select('.main-text-wrap .read-content p'); # 通過打印ct,再次確認元素選擇器結構print(len(sps)); # 89:89個自然段的內容cts = [];# 遍歷,獲取每一個內容,放入到cts中for p in sps:cts.append(p.get_text());# print(cts);# 寫入文件f = open(r'C:\Users\lwx\Desktop\星門\{}'.format(k + '.txt'), 'w');f.write('\n'.join(cts)); # 將cts中所有字符串拼接,使用\n換行,間隔f.close();print("已下載:" + k)3.2、爬取數據接口-英雄之刃頭像
import requests from bs4 import BeautifulSoup as BS# 驗證,爬取到的就是一個殼子,其中沒有英雄信息!!!# 偽裝成瀏覽器 hds = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36'}url = 'https://cos.99.com/data/'resp = requests.get(url, headers=hds);ct = resp.content.decode('utf-8');# print(ct) # print(ct.index('東方月初')) # 報錯bs = BS(ct, 'lxml'); # 解析htmlimgs = bs.select('#heroList .img img'); # print(len(imgs)) # 0# 數據是通過ajax來訪問后臺數據接口得到的# 所以爬蟲程序,需要找到數據接口,來訪問得到數據 # 關鍵: 通過瀏覽器調試工具 找數據接口!!!# 方法:綜合根據 接口名、 篩選工具(Fetch/XHR)、響應內容等方法,來找到數據接口# 英雄信息的數據接口: # https://wjdown.99.com/games/cos/upload/yhzrheroattr/yhzr_hero_list.js?_=1641864636638# 爬取數據接口,得到英雄信息,并解析: import json# 爬取數據接口,得到數據hds = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36'}url = 'https://wjdown.99.com/games/cos/upload/yhzrheroattr/yhzr_hero_list.js?_=1641864636638'resp = requests.get(url, headers=hds);ct = resp.content.decode('utf-8'); # print(ct) # print(type(ct)) # 解析接口響應的字符串,不是html頁面,不需要使用BeautifulSoup# 對象形式的字符串---->對象本身ls = json.loads(ct); # print(ls) # print(type(ls))dr = r'C:\Users\lwx\Desktop\英魂之刃\{}.jpg'for h in ls:# print(h['name'])# print(h['headimg'])# https://wjdown.99.com/games/cos/upload/yhzrheroheadimg/123001.jpgresp = requests.get(h['headimg'], headers=hds);ct = resp.content; # 文件格式數據,不需要decode解碼f = open(dr.format(h['name']), 'wb');f.write(ct);f.close();print('下載完成:', h['name']);小結,根據爬取后的響應,分為如下3種情況:
3.3、爬取數據接口-課堂練習-王者頭像
課堂練習:將王者中108個英雄的頭像全部爬取下來,文件名以英雄的名字來命名:https://pvp.qq.com/web201605/herolist.shtml
提醒:
心為形役,塵世馬牛;身被名牽,樊籠雞鶩。——明·陳繼儒《小窗幽記》
勿自暴,勿自棄,圣與賢,可馴致。——《弟子規》
正氣內存,邪不可干。——《黃帝內經》
總結
以上是生活随笔為你收集整理的Python实训day06am【网络爬虫(爬取接口)】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python实训day05pm【JS-D
- 下一篇: Python实训day06pm【网络爬虫