Python 十一 原生爬蟲

2021-08-30 08:56:03 字數 3924 閱讀 1601

一、分析抓取目的確定抓取頁面

#爬取主播人氣排行

二、整理爬蟲常規思路

爬蟲前奏

明確目的

找到資料對應的網頁

分析網頁的結構找到資料所在的標籤位置

模擬 http 請求, 向伺服器傳送這個請求, 獲取到伺服器返回給我們的html

用正規表示式提取我們要的資料(名字,人數)

三、 vscode中除錯**

f5 啟動 和vs 除錯一樣

beautifulsoup , scrapy

爬蟲、 反爬蟲、反反爬蟲

ip 封

** ip庫

五、資料提取層級分析及原則

三、正則分析html、正則分析獲取名字和人數

from urllib import request

import re

#斷點除錯 有坑 7i

class spider():

url = ''

root_pattern ='([\s\s]*?)

' name_pattern = '([\s\s]*?)'

number_pattern = '([\s\s]*?)'

def __fetch_content(self):

r = request.urlopen(spider.url)

#bytes

htmls = r.read()

htmls = str(htmls,encoding='utf-8')

return htmls

def __analysis(self,htmls):

root_html = re.findall(spider.root_pattern, htmls)

anchors =

for html in root_html:

name = re.findall(spider.name_pattern, html)

number = re.findall(spider.number_pattern, html)

anchor =

# print(anchors[0])

return anchors

def __refine(self, anchors):

l = lambda anchor:

return map(l,anchors)

def go(self):

htmls = self.__fetch_content()

anchors = self.__analysis(htmls)

anchors =list(self.__refine(anchors))

print(anchors[0])

s = spider()

s.go()

結果:

八、 資料精煉、 sorted 排序

from urllib import request

import re

#斷點除錯 坑 7i

class spider():

url = ''

root_pattern ='([\s\s]*?)

' name_pattern = '([\s\s]*?)'

number_pattern = '([\s\s]*?)'

# 獲取資料的頁面

def __fetch_content(self):

r = request.urlopen(spider.url)

#bytes

htmls = r.read()

htmls = str(htmls,encoding='utf-8')

return htmls

# 從頁面上抓取資料

def __analysis(self,htmls):

root_html = re.findall(spider.root_pattern, htmls)

anchors =

for html in root_html:

name = re.findall(spider.name_pattern, html)

number = re.findall(spider.number_pattern, html)

anchor =

# print(anchors[0])

return anchors

# 資料取雜質(空格換行)strip() 字串去空格換行

def __refine(self, anchors):

l = lambda anchor:

return map(l,anchors) #map類 對字典每乙個序列進行l這個函式

# 對抓取的資料進行排序 reverse=true 倒序

def __sort(self, anchors):

anchors = sorted(anchors, key=self.__sort_seed, reverse=true)

return anchors

# 給 key 寫的函式 說明用那個進行排序

def __sort_seed(self, anchors):

r = re.findall('\d*', anchors['number'])

number = float(r[0])

if '萬' in anchors['number']:

number *= 10000

return number

# 顯示排名

def __show(self, anchors):

for rank in range(0,len(anchors)):

print('rank '+ str(rank +1)+' : '+anchors[rank]['name']+' '+anchors[rank]['number']+'人')

# 主程式

def go(self):

htmls = self.__fetch_content()

anchors = self.__analysis(htmls)

anchors =list(self.__refine(anchors))

print(anchors[0])

anchors= self.__sort(anchors)

self.__show(anchors[:20])

s = spider()

s.go()

結果:rank 1 : 賈克虎丶虎神 96.9萬人

rank 2 : lol丶搖擺哥 20.2萬人

rank 3 : lpl熊貓官方直播 12.1萬人

rank 4 : wucg官方直播平台 8.4萬人

rank 5 : 溫州丶黃小明 5.1萬人

rank 6 : 暴君aa 4.6萬人

rank 7 : 順順套路王 3.1萬人

rank 8 : 火苗ob解說 2.5萬人

rank 9 : 蘭晨丶 1.1萬人

rank 10 : 海洋ovo 1.9萬人

rank 11 : 小馬哥玩蓋倫 1.6萬人

rank 12 : 牛老師丶 1.5萬人

rank 13 : riot國際賽事直播間 1.5萬人

rank 14 : 小白mini 7361人

rank 15 : 乙個很c的稻草人 7223人

rank 16 : 抗寒使者 4976人

rank 17 : 小麥子鮮忌廉 4902人

rank 18 : 祝允兒 4574人

rank 19 : 請叫我大腿巖丶 4201人

rank 20 : 李小青zj 3838人

Python 十一 原生爬蟲

一 分析抓取目的確定抓取頁面 爬取主播人氣排行 二 整理爬蟲常規思路 爬蟲前奏 明確目的 找到資料對應的網頁 分析網頁的結構找到資料所在的標籤位置 模擬 http 請求,向伺服器傳送這個請求,獲取到伺服器返回給我們的html 用正規表示式提取我們要的資料 名字,人數 三 vscode中除錯 f5 啟...

實戰 原生爬蟲

2.找到資料對應的網頁,充分了解所爬 的結構,確定爬取頁面和內容,找到資料所在的標籤位置 在谷歌瀏覽器按f12可出現htmls 3.怎麼爬 模擬http請求,向伺服器傳送這個請求,獲取到伺服器返回給我們的html 用正規表示式提取我們要的資料 名字,人氣 1.找定位標籤 盡量具有唯一性,盡量最接近要...

原生js大總結十一

101 請簡述prototype proto constructor三者的關係 1 prototype 每乙個函式都有乙個prototype這個屬性,而這個屬性指向乙個物件,這個物件我們叫做原型物件 作用 a 節約記憶體 b 擴充套件屬性和方法 c 可以實現類之間的繼承 2 proto 1 每乙個物...