Python 原生爬蟲

來源:互聯網
上載者:User

標籤:發送   strip()   pid   ora   inf   span   spider   odi   urllib   

Python3 實現遊戲主播人氣熱門排行榜
from urllib import requestimport re# 爬取某個遊戲主播的人氣(每個遊戲主播的觀看人數)熱門排行榜‘‘‘爬蟲前奏:    明確爬蟲目的    找到資料對應的網頁    分析網頁的結構找到資料所在的標籤位置    類比HTTP請求,向伺服器發送這個請求,擷取到伺服器返回給我們的HTML    利用Regex提取我們要的資料(主播名字,人氣)概括字元集:    \d \D    \w 單詞字元 \W    \s 空白字元 \S    . 匹配除分行符號\n之外的所有字元爬蟲架構:    Scrapy    BeautifulSoup進階:    爬蟲    大資料的儲存    資料的分析常見問題:    反爬蟲    反反爬蟲    IP被封    代理IP庫‘‘‘class Spider():    url = ‘https://www.panda.tv/cate/lol‘    root_pattern = ‘<div class="video-info">([\s\S]*?)</div>‘    name_pattern = ‘</i>([\s\S]*?)</span>‘    number_pattern = ‘<span class="video-number">([\s\S]*?)</span>‘    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 = {‘name‘: name, ‘number‘: number}            anchors.append(anchor)        print(anchors[0])        return anchors    def __refine(self, anchors):        jl = lambda anchors: {            ‘name‘: anchors[‘name‘][0].strip(),            ‘number‘: anchors[‘number‘][0].strip()            }        return map(jl, anchors)    def __sort(self, anchors):        # filter        anchors = sorted(anchors, key=self.__sort_seed, reverse=True)        return anchors    def __sort_seed(self, anchor):        r = re.findall(‘\d*\.\d*‘, anchor[‘number‘])        number = float(r[0])        if ‘萬‘ in anchor[‘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))        anchors = self.__sort(anchors)        self.__show(anchors)spider = Spider()spider.go()

 

 

Python 原生爬蟲

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.