爬蟲1_2019年豆瓣新片排行榜

from bs4 import BeautifulSoup
import requests
import lxml


def request_douban(url):
    try:
        headers = {'user-agent': 'my-app/0.0.1'}  # 僞裝成瀏覽器,避免403,被禁止訪問
        response = requests.get(url, headers=headers)
        if response.status_code == 200:
            return response.text
    except requests.RequestException:
        return None


def main(url):
    html = request_douban(url)
    soup = BeautifulSoup(html, 'lxml')  # 源碼
    moives_list = soup.find(class_="").find_all('tr')
    # print(moives_list)
    for item in moives_list:
        # print(item)
        # item_name = item.find(class_='title').string
        try:
            item_name = item.find(class_='nbg').get('title')
            item_img = item.find('a').find('img').get('src')
            item_score = item.find(class_="rating_nums").string
            item_comment = item.find(class_="star clearfix").find(class_="pl").string
            print('電影名:{:<10}評分:{:<5}評價人數:{:<10}封面:{}'.format(item_name, item_score, item_comment, item_img))
            print('-'*50)
        except:
            print('Error')


if __name__ == "__main__":
    url = 'https://movie.douban.com/chart'
    main(url)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章