Python 爬蟲入門執行個體(爬取小米市集的top應用apk)

來源:互聯網
上載者:User

標籤:while   val   down   download   plist   code   代碼   compile   熱門排行榜   

一,爬蟲是什嗎?

爬蟲就是擷取網路上各種資源,資料的一種工具。具體的可以自行百度。

二,如何寫簡單爬蟲

1,擷取網頁內容

可以通過 Python(3.x) 內建的 urllib,來實現網頁內容的下載。實現起來很簡單

import urllib.requesturl="http://www.baidu.com"response=urllib.request.urlopen(url)html_content=response.read()

 還可以使用三方庫 requests ,實現起來也非常方便,在使用之前當然你需要先安裝這個庫:pip install requests 即可(Python 3以後的pip非常好使)

 

import requestshtml_content=requests.get(url).text

2, 解析網頁內容

擷取的網頁內容html_content,其實就是html代碼,我們需要對其進行解析,擷取我們所需要的內容。

解析網頁的方法有很多,這裡我介紹的是BeautifullSoup,由於這是一個三方庫,在使用前 還是要先安裝 :pip install bs4

form bs4 imort BeautifullSoupsoup= BeautifullSoup(html_content, "html.parser")

三,執行個體分析

弄懂爬蟲原理的最好辦法,就是多分析一些執行個體,爬蟲千變萬化,萬變不離其宗。廢話少說上乾貨。

===================================我是分割線===================================================

需求:爬取小米市集的TOP n 應用

通過瀏覽器開啟小米市集排行棒頁面,F12審查元素

 

#coding=utf-8import requests
import refrom bs4 import BeautifullSoup def parser_apks(self, count=0): ‘‘‘小米應用市場‘‘‘ _root_url="http://app.mi.com" #應用市場首頁網址 res_parser={} page_num=1 #設定爬取的頁面,從第一頁開始爬取,第一頁爬完爬取第二頁,以此類推 while count:
#擷取熱門排行榜頁面的網頁內容 wbdata = requests.get("http://app.mi.com/topList?page="+str(page_num)).text print("開始爬取第"+str(page_num)+"頁")
#解析頁面內容擷取 應用下載的 介面串連 soup=BeautifulSoup(wbdata,"html.parser") links=soup.body.contents[3].find_all("a",href=re.compile("/details?"), class_ ="", alt="") #BeautifullSoup的具體用法請百度一下吧。。。 for link in links: detail_link=urllib.parse.urljoin(_root_url, str(link["href"])) package_name=detail_link.split("=")[1]
#在下載頁面中擷取 apk下載的地址 download_page=requests.get(detail_link).text soup1=BeautifulSoup(download_page,"html.parser") download_link=soup1.find(class_="download")["href"] download_url=urllib.parse.urljoin(_root_url, str(download_link))
#解析後會有重複的結果,下面通過判斷去重 if download_url not in res_parser.values(): res_parser[package_name]=download_url count=count-1 if count==0: break if count >0: page_num=page_num+1 print("爬取apk數量為: "+str(len(res_parser))) return res_parser
def craw_apks(self, count=1, save_path="d:\\apk\\"):        res_dic=parser_apks(count)                for apk in res_dic.keys():            print("正在下載應用: "+apk)            urllib.request.urlretrieve(res_dic[apk],save_path+apk+".apk")            print("下載完成")
if __name__=="__main__":

craw_apks(10)

運行結果:

開始爬取第1頁爬取apk數量為: 10正在下載應用: com.tencent.tmgp.sgame
下載完成
.
.
.

以上就是簡單爬蟲的內容,其實爬蟲的實現還是很複雜的,不同的網頁有不同的解析方式,還需要深入學習。。。

 

Python 爬蟲入門執行個體(爬取小米市集的top應用apk)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.