第13章所講重點(diǎn)為:原生爬蟲
13-1分析抓取目的確定抓取頁(yè)面
# 13-1分析抓取目的確定抓取頁(yè)面
# 13-2 整理爬蟲常規(guī)思路
爬蟲前奏:
明確目的
找到數(shù)據(jù)對(duì)應(yīng)的網(wǎng)頁(yè)
分析網(wǎng)頁(yè)的結(jié)構(gòu)找到數(shù)據(jù)所在的標(biāo)簽位置
模擬HTTP請(qǐng)求, 向服務(wù)器發(fā)送這個(gè)請(qǐng)求,獲取到服務(wù)區(qū)返回給我們的HTML
用正則表達(dá)式提取我們需要的數(shù)據(jù)(名字,人氣)
# 13-3 VSCode中調(diào)試代碼
import re
from urllib import request #通過Python內(nèi)置的庫(kù)來模仿HTTP請(qǐng)求獲取要爬取的網(wǎng)頁(yè)
class Spider():
url = 'https://www.panda.tv/cate/lol'#保存要抓取的地址
root_pattern = '<div> class="video-info">[\s\S]*?</div>'
def __fetch_content(self):#獲取網(wǎng)頁(yè)的內(nèi)容,實(shí)例方法需要加self
r = request.urlopen(Spider.url)#通過request下面的方法來獲取網(wǎng)頁(yè)信息,并且將信息傳遞進(jìn)來
htmls = r.read()#將網(wǎng)頁(yè)內(nèi)容讀取出來
htmls = str(htmls,encoding = 'utf-8')#文本轉(zhuǎn)換
return htmls
def __analysis(self,htmls):#分析文本
root_html = re.findall(Spider.root_pattern,htmls)
def go(self):#入口方法
htmls = self.__fetch_content()
self.__analysis(htmls)
spider = Spider()
spider.go()
# 13-4 HTML結(jié)構(gòu)分析基本原則二條
就近原則
特定的標(biāo)識(shí)符或者標(biāo)簽!
# 13-4 HTML結(jié)構(gòu)分析基本原則二條
盡量選擇可以閉合的標(biāo)簽作為定位標(biāo)簽
# 13-6 正則分析HTML
import re
from urllib import request #通過Python內(nèi)置的庫(kù)來模仿HTTP請(qǐng)求獲取要爬取的網(wǎng)頁(yè)
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
class Spider():
url = 'https://www.panda.tv/cate/lol'#保存要抓取的地址
root_pattern = '<div class="video-info">([\s\S]*?)</div>'#注意用正則表達(dá)式分析我們所提取的內(nèi)容
name_pattern = '</i>([\s\S]*?)</span>'#根據(jù)思維導(dǎo)圖第二步,名字匹配
number_pattern = '<span class="video-number">([\s\S]*?)</span>'#根據(jù)思維導(dǎo)圖第二步,人數(shù)匹配
def __fetch_content(self):#獲取網(wǎng)頁(yè)的內(nèi)容,實(shí)例方法需要加self
r = request.urlopen(Spider.url)#通過request下面的方法來獲取網(wǎng)頁(yè)信息,并且將信息傳遞進(jìn)來
htmls = r.read()#將網(wǎng)頁(yè)內(nèi)容讀取出來
htmls = str(htmls,encoding = 'utf-8')#將字節(jié)碼轉(zhuǎn)換成文本方法
return htmls
def __analysis(self,htmls):#定義一個(gè)實(shí)例方法。分析文本,進(jìn)入網(wǎng)頁(yè)分析頁(yè)面信息
root_html = re.findall(Spider.root_pattern,htmls)
anchors = []#定義一個(gè)anchors的列表
for html in root_html:#for循環(huán)遍歷
name = re.findall(Spider.name_pattern,html)
number = re.findall(Spider.number_pattern,html)
anchor = {'name':name,'number':number}#打印成字典形式
anchors.append(anchor)#在列表中添加元素的方法
# print(root_html[0])#打印一個(gè)結(jié)果看看,進(jìn)一步分析我們所需要的數(shù)據(jù),如圖5
# print(anchors[0])
return anchors
def __refine(self,anchors):#爬蟲里面最精煉的一個(gè)環(huán)節(jié),精煉環(huán)節(jié).去掉空格換行符
l = lambda anchor:{
'name':anchor['name'][0].strip(),
'number':anchor['number'][0]
}#lambda函數(shù)配合內(nèi)置函數(shù)
return map(l,anchors)
def __sort(self,anchors):#排序
anchors = sorted(anchors,key=self.__sort_seed,reverse = True)#使用sorted的注意事項(xiàng),一定要指定key的值
return anchors
def __sort_seed(self,anchor):
r = re.findall('\d*',anchor['number'])
number = float(r[0])
if '萬(wàn)' in anchor['number']:
number *=10000
return number
def __show(self,anchors):#展示打印結(jié)果
for rank in range(0,len(anchors)):
# print(anchor['name'] + '-----' + anchor['number'])
print('rank' + str(rank + 1)
+ ' : ' + anchors[rank]['name']
+ ' ' + anchors[rank]['number'])
def go(self):#入口方法。所有的方法都會(huì)經(jīng)過go方法總控
htmls = self.__fetch_content()
anchors = self.__analysis(htmls)#傳入anchors
anchors = list(self.__refine(anchors))
anchors = self.__sort(anchors)
self.__show(anchors)
spider = Spider()
spider.go()
# 總結(jié)如下:
'''
beautifulsoup
scrapy
'''

image

image

image

image

image
打印效果如下:

image
在做爬蟲的時(shí)候Python3中很有可能會(huì)出現(xiàn)如圖1的情況:

image
解決辦法如圖2的紅框部分,把紅框上的代碼添加上去以后問題即可解決

image