爬取豆瓣电影评分前250部电影


# 豆瓣Top250
import requests
from bs4 import BeautifulSoup


def get_movie():
    movie_list = []
    headers = {
        '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',
        'Host': 'movie.douban.com'
    }
    for i in range(0, 10):
        link = 'https://movie.douban.com/top250?start=' + str(i * 25)
        r = requests.get(link, headers=headers, timeout=10)
        print(str(i + 1) + "页的响应状态码", r.status_code)

        soup = BeautifulSoup(r.text, "lxml")
        print("网址:{},网页内容:{}".format(link,soup))


        div_list = soup.find_all('div', class_='hd')
        for each in div_list:
            movie = each.a.span.text.strip()
            movie_list.append(movie)
    return movie_list


if __name__ == "__main__":
    a = get_movie()
    print(a)

爬取豆瓣电影评分前250部电影_第1张图片

你可能感兴趣的:(爬虫)