栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python爬取豆瓣Top250电影信息

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python爬取豆瓣Top250电影信息

1、爬取豆瓣Top250电影信息以文本形式保存 数据:豆瓣电影

2、代码

import requests
from bs4 import BeautifulSoup


def get_movie():
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36 SE 2.X metaSr 1.0'}  # 创建头部信息
    movie_list=[['序号','标题','推荐语','演员','链接']]
    for i in range(0,10):
        url = 'https://movie.douban.com/top250?start='+str(i*25)
        response=requests.get(url,headers=headers)
        soup=BeautifulSoup(response.text,"html.parser")
        ol = soup.find('ol', class_='grid_view')
        li_list=ol.find_all('li')
        for each in li_list:
            no=each.find('em').text
            title = each.find('span', class_="title").text
            inq = each.find('span', class_='inq')
            star = each.find('p').text
            href=each.find('a')['href']
            movie_list.append([no,title,inq.text if inq!=None else '',star,href])
    return movie_list
movie=[]
movie=get_movie()
with open("Top_movie_250.txt","a+",encoding="utf-8") as f:
    for i in movie:
        f.write(str(i))
        f.write("n")
    f.close()
movie.py
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/283658.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号