python爬虫 —— 豆瓣电影top250电影

 代码很简单,后续再找更好的例子吧。

import requests
from bs4 import BeautifulSoup

def get_moves():
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36','Host':'movie.douban.com'}
    movie_list = []
    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 ("{0:2}".format(str(i + 1)), "页响应状态码:", r.status_code)
        
        soup = BeautifulSoup(r.text, "html")
        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__':
    movies = get_moves()
    print (movies)

运行结果:

发布了19 篇原创文章 · 获赞 9 · 访问量 2995

猜你喜欢

转载自blog.csdn.net/Necrolic/article/details/105557511