日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

萌新用Python嘗試爬豆瓣top250

系統 1687 0

這是我在CSDN的第一篇博客,假期剛自學Python,嘗試爬取了一下豆瓣top250.
希望可以有大佬指點
感謝

            
              import requests
from bs4 import BeautifulSoup


def gethtml(url):
    try:
        kv = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'}

        r=requests.get(url,headers=kv,timeout=30)
        r.raise_for_status()
        r.encoding=r.apparent_encoding
        
        return r.text
    except:
        print("fault")
    

def jiexi(list1,list2,list3,html):
    soup=BeautifulSoup(html,"html.parser")
    div_list=soup.find_all('div', class_='hd')
    div_list1=soup.find_all('div',class_='bd')
    div_list2=soup.find_all('div',class_='star')
    for each in div_list:
        movie=each.a.span.text
        list1.append(movie)
    for each in div_list1:
        yy=each.p.text.strip()
        if len(yy)>2:
            list2.append(yy)

    for each in div_list2:
        fen=each.contents
        list3.append((fen[3]).text)

    
    

def printlist(list1,list2,list3):
    print("豆瓣排名電影")
    print(len(list1))
    for i in range(len(list1)):
        print("排名"+str(i+1))
        print("名字:"+list1[i])
        print("詳情:"+list2[i])    
        print("評分:"+list3[i])
        print("--------------------------------------------------------------------------")

def main():
    url0="https://movie.douban.com/top250?start="
    
    list1=[]
    list2=[]
    list3=[]

    for i in range(4):    #4是爬取的頁數
        try:

            url=url0+str(i*25)
            html=gethtml(url)
            jiexi(list1,list2,list3,html)
        except:
            continue
    printlist(list1,list2,list3)
        

main()

            
          

更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 芒康县| 阜阳市| 贵阳市| 三都| 泗阳县| 名山县| 康定县| 奎屯市| 百色市| 应城市| 宝应县| 娄烦县| 修文县| 英超| 高邮市| 基隆市| 阳信县| 泾源县| 凤凰县| 平陆县| 渝中区| 依安县| 娄底市| 越西县| 屏东县| 白城市| 晋江市| 宜昌市| 美姑县| 合川市| 合江县| 紫阳县| 青铜峡市| 大方县| 乐昌市| 教育| 剑川县| 深圳市| 华池县| 荣昌县| 道真|