1. 程式人生 > >Python爬蟲爬取OA幸運飛艇平臺獲取數據

Python爬蟲爬取OA幸運飛艇平臺獲取數據

sta 獲取數據 status fail attrs color wrapper 排行榜 req

安裝BeautifulSoup以及requests

打開window 的cmd窗口輸入命令pip install requests 執行安裝,等待他安裝完成就可以了

BeautifulSoup庫也是同樣的方法

我使用的編譯器的是sublime text 3,覺得是挺好用的一個編譯軟件

其他工具: Chrome瀏覽器

Python版本: Python3.6

運行平臺: Windows

1、首先我們搜索OA幸運飛艇平臺排行榜:【×××。com/h5】企 娥:217 1793 408
技術分享圖片
獲取網頁的代碼:

[python] view plain copy
def getHTMLText(url,k):
try:

if(k==0):
a={}
else:
a={‘offset‘:k}
r = requests.get(url,params=a,headers={‘User-Agent‘: ‘Mozilla/4.0‘})
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
print("Failed!")
經過觀察其中因為每一頁的網址其offset都不相同,故只要改變offset=k便可獲取每一頁的信息

通過main函數以改變URL:

[python] view plain copy

def main():
basicurl=‘×××。com/h5‘
k=0
while k<=100:
html=getHTMLText(basicurl,k)
k+=10
getname(html)
通過BeautifulSoup的方法層層獲取標簽中的信息,並for循環輸出

[python] view plain copy
def getname(html):
soup = BeautifulSoup(html, "html.parser")
paihangList=soup.find(‘dl‘,attrs={‘class‘:‘board-wrapper‘})

mov=[]
actor=[]
for movlist in paihangList.find_all(‘dd‘):
movitem=movlist.find(‘div‘,attrs={‘class‘:‘movie-item-info‘})
movname=movitem.find(‘p‘,attrs={‘class‘:‘name‘}).getText()
actors=movlist.find(‘div‘,attrs={‘class‘:‘board-item-main‘})
actorname=actors.find(‘p‘,attrs={‘class‘:‘star‘}).getText()
b=actorname.replace(‘\n‘,‘‘)
c=b.replace(‘ ‘,‘‘)
actor.append(c)
mov.append(movname)
mode= "{0:<30}\t{1:<50}"
for i,j in zip(mov,actor):
print(mode.format(i,j,chr(12288)))

Python爬蟲爬取OA幸運飛艇平臺獲取數據