爬取貓眼top100榜單電影

今天終于靜下心來(lái)學(xué)習(xí)python爬蟲(chóng)了,寫(xiě)了一個(gè)爬取貓眼top100榜單電影的小爬蟲(chóng),效率不高,下次一定加油。

一張效果圖(1~100)


都是上學(xué)期學(xué)過(guò)的,現(xiàn)在復(fù)習(xí)復(fù)習(xí)

上代碼

import requests

from bs4 import BeautifulSoup

import urllib

def download(url, num_retries=2):

try:

headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3278.0 Safari/537.36'}

html = requests.get(url, headers=headers)

'''html.encoding=gb2312'''

except Exception as e:

print ('Download error:', e.reason)

html = None

if num_retries > 0:

if 500 <= html.status_code < 600:

return download(url, num_retries-1)

return html

def tiqu(html):

contents = []

bsobject = BeautifulSoup(html, "lxml")

caption = bsobject.findAll('p',{"class":'board-content'})[0].string

for lists in bsobject.findAll('dl',{'class':'board-wrapper'})[0]:

score = ''

content = []

if len(lists) == 1:

continue

content.append(lists.find('i').string)

test = lists.findAll('p',)

for aa in test:

bb = str(aa.string)

bb = bb.replace('\n','').strip()

if bb == 'None':

continue

content.append(bb)

for num in test[3]:

score = score + num.string

content.append(float(score.strip()))

contents.append(content)

return caption, contents

def getUrl(url, html):

urllist = []

listobject = BeautifulSoup(html, 'lxml')

urlone = listobject.findAll('a', {"class":'page_2'})[0].attrs['href'][:-2]

for num in range(0,100,10):

urls = urlone + str(num)

urllist.append(urllib.parse.urljoin(url, urls))

return urllist

def main():

contents = []

url = "http://maoyan.com/board/4"

html = download(url)

urllist = getUrl(url, html.text)

for url in urllist:

html = download(url)

caption , content = tiqu(html.text)

for con in content:

contents.append(con)

for con in contents:

print(con)

if __name__ == '__main__':

main()

看起來(lái)效果不好,還是上傳文件吧

鏈接:https://pan.baidu.com/s/1VBFH4RUagRMgIINT30xP7g 密碼:tydw

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一,HTML語(yǔ)言的一般語(yǔ)法: 1,圍堵標(biāo)記:<>… 1)帶屬性的標(biāo)記: … 2)無(wú)屬性的標(biāo)記:加粗 居中 標(biāo)題 2...
    清水易藍(lán)閱讀 1,380評(píng)論 0 2
  • 圖表控件庫(kù) MPAndroidChart 的使用 使用方法 項(xiàng)目源碼地址,包含了很多類(lèi)型的圖標(biāo) https://g...
    jinchuang閱讀 889評(píng)論 0 0
  • Fastlane是一套使用Ruby寫(xiě)的自動(dòng)化工具集,用于iOS和Android的自動(dòng)化打包、發(fā)布等工作,可以節(jié)省大...
    刃之劍閱讀 275評(píng)論 0 0
  • 來(lái)到大學(xué),不再像高中那樣,自己可以有了很多的課余時(shí)間。然而很多人卻因?yàn)榉N種原因而荒蕪了這些時(shí)間。 這是因?yàn)楹芏鄷r(shí)候...
    木行上閱讀 1,056評(píng)論 0 0
  • 人這一生,既不像想的那么好,也不像想的那么壞。 小學(xué)的時(shí)候,為了應(yīng)付暑假閱讀的規(guī)定,瀏覽了一下莫泊桑的《一生》,后...
    更欣閱讀 876評(píng)論 0 0

友情鏈接更多精彩內(nèi)容