其他分享
首页 > 其他分享> > 爬取猫眼上近期上映的电影及评分信息

爬取猫眼上近期上映的电影及评分信息

作者:互联网

爬取猫眼上近期上映的电影及评分信息

文章目录


本次爬虫使用的是re,requests和bs库,如果没有,可以使用pip命令安装

1.网页分析

目标网址为:https://maoyan.com/films?showType=2&offset= 在这里插入图片描述
右键点击审查元素,可以看到,电影的信息是被存放在名为‘movie-item-hover’的标签下,图片信息存放在‘moive-poster’的标签下
在这里插入图片描述

2.相关代码

代码如下:

import re
from bs4 import BeautifulSoup
import requests
import time
import pandas as pd

url_maoyan="https://maoyan.com/films?showType=2&offset="
names,tags,actors,times=[],[],[],[]
headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
            'Cookie':'lianjia_uuid=9d3277d3-58e4-440e-bade-5069cb5203a4; UM_distinctid=16ba37f7160390-05f17711c11c3e-454c0b2b-100200-16ba37f716618b; _smt_uid=5d176c66.5119839a; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2216ba37f7a942a6-0671dfdde0398a-454c0b2b-1049088-16ba37f7a95409%22%2C%22%24device_id%22%3A%2216ba37f7a942a6-0671dfdde0398a-454c0b2b-1049088-16ba37f7a95409%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%7D%7D; _ga=GA1.2.1772719071.1561816174; Hm_lvt_9152f8221cb6243a53c83b956842be8a=1561822858; _jzqa=1.2532744094467475000.1561816167.1561822858.1561870561.3; CNZZDATA1253477573=987273979-1561811144-%7C1561865554; CNZZDATA1254525948=879163647-1561815364-%7C1561869382; CNZZDATA1255633284=1986996647-1561812900-%7C1561866923; CNZZDATA1255604082=891570058-1561813905-%7C1561866148; _qzja=1.1577983579.1561816168942.1561822857520.1561870561449.1561870561449.1561870847908.0.0.0.7.3; select_city=110000; lianjia_ssid=4e1fa281-1ebf-e1c1-ac56-32b3ec83f7ca; srcid=eyJ0Ijoie1wiZGF0YVwiOlwiMzQ2MDU5ZTQ0OWY4N2RiOTE4NjQ5YmQ0ZGRlMDAyZmFhODZmNjI1ZDQyNWU0OGQ3MjE3Yzk5NzFiYTY4ODM4ZThiZDNhZjliNGU4ODM4M2M3ODZhNDNiNjM1NzMzNjQ4ODY3MWVhMWFmNzFjMDVmMDY4NWMyMTM3MjIxYjBmYzhkYWE1MzIyNzFlOGMyOWFiYmQwZjBjYjcyNmIwOWEwYTNlMTY2MDI1NjkyOTBkNjQ1ZDkwNGM5ZDhkYTIyODU0ZmQzZjhjODhlNGQ1NGRkZTA0ZTBlZDFiNmIxOTE2YmU1NTIxNzhhMGQ3Yzk0ZjQ4NDBlZWI0YjlhYzFiYmJlZjJlNDQ5MDdlNzcxMzAwMmM1ODBlZDJkNmIwZmY0NDAwYmQxNjNjZDlhNmJkNDk3NGMzOTQxNTdkYjZlMjJkYjAxYjIzNjdmYzhiNzMxZDA1MGJlNjBmNzQxMTZjNDIzNFwiLFwia2V5X2lkXCI6XCIxXCIsXCJzaWduXCI6XCIzMGJlNDJiN1wifSIsInIiOiJodHRwczovL2JqLmxpYW5qaWEuY29tL3p1ZmFuZy9yY28zMS8iLCJvcyI6IndlYiIsInYiOiIwLjEifQ=='
            }
for i in range(4):
    url=url_maoyan+str(i*30)
    html=requests.get(url,headers=headers)
    html.encoding='utf-8'
    html=html.text
    bs=BeautifulSoup(html,'html.parser')
    moive_info=bs.find_all(class_="movie-hover-info")
    find_movie=re.compile(r'<span class="name noscore">(.*?)</span>')
    find_info=re.compile(r'class="hover-tag".*?</span>(.*?)</div>',re.S)
    find_time=re.compile(r'class="hover-tag">上映时间:</span>\n(.*?)</div>',re.S)
    for item in moive_info:
        name=re.findall(find_movie,str(item))
        tag=re.findall(find_info,str(item))[0]
        actor=re.findall(find_info,str(item))[1]
        date=re.findall(find_time,str(item))
        times.append(date)
        names.append(name)
        actors.append(actor)
        tags.append(tag)  

data_movie=pd.DataFrame({'name':names,'tag':tags,'actor':actors,'time':times})
print(data_movie)
data_movie.to_csv('maoyan.csv',encoding='utf_8_sig')

3.总结

由于访问次数只有4次,没有设置等待时间,如果访问次数过多,需要设置等待时间以防止访问次数过多出现验证。

标签:item,movie,22%,3A%,爬取,re,上映,find,猫眼
来源: https://blog.csdn.net/qq_44988175/article/details/114782098