猫眼电影你好像被盯上了

python爬取猫眼电影top_100

初学者专用,超基础
import re
import requests
from urllib3.exceptions import RequestError
import csv
import pandas as pd


def get_one_page(url):
    try:
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36'
        }
        response = requests.get(url, headers=headers)
        if response.status_code == 200:
            #print(response.text)
            return response.text
        return None
    except RequestError:
        return None


def parse_one_page(html):
    pattern = re.compile('<dd>.*?board-index.*?>(\d+)</i>.*?data-src="(.*?)".*?name"><a'
                         + '.*?>(.*?)</a>.*?star">(.*?)</p>.*?releasetime">(.*?)</p>'
                         + '.*?integer">(.*?)</i>.*?fraction">(.*?)</i>.*?</dd>', re.S)
    items = re.findall(pattern, html)
    #print(items)
    index = []
    image = []
    title = []
    actor = []
    time = []
    score = []
    for item in items:
        index.append(item[0])
        image.append(item[1])
        title.append(item[2])
        actor.append(item[3].strip()[3:])
        time.append(item[4].strip()[5:])
        score.append(item[5] + item[6])
    #print(index,image,title,actor,time,score)
    return index,image,title,actor,time,score

def write_to_file(index,image,title,actor,time,score):
    test = pd.DataFrame({"index": index, 'image': image, 'title': title, 'actor': actor, 'time': time, 'score':score})
    test.to_csv('MaoY_top10_movie.csv', sep=',', mode='a', encoding='utf-8-sig', index=False, header=0)

def add_file_head():
    with open('MaoY_top10_movie.csv','w',newline='',encoding='utf-8-sig') as f:
        writer = csv.writer(f)
        writer.writerow(["index", "image","title","actor","time","score"])

def main(offset):
    url = "http://maoyan.com/board/4?offset=" + str(offset)
    html = get_one_page(url)
    parse_one_page(html)
    write_to_file(parse_one_page(html)[0],
                  parse_one_page(html)[1],
                  parse_one_page(html)[2],
                  parse_one_page(html)[3],
                  parse_one_page(html)[4],
                  parse_one_page(html)[5])

if __name__ == '__main__':
    add_file_head()
    for i in range(10):
        main(i*10)


全部评论

相关推荐

今年要就业的同学早做打算。我们招的实习生现在全是985硕士了,四年前找的全是二本本科生。
AI牛可乐:哎呀,就业市场确实挺有挑战性的呢。不过,学长学姐们越来越厉害了,985硕士听起来就很高大上呢!那四年前和现在的变化好大呀,你觉得是什么原因让企业更倾向于招聘高学历的同学呢?😊 如果不介意的话,想问问你是做什么行业的呀?悄悄告诉你,点击我的头像,我们可以私信聊聊哦,那里更方便呢!🐮🎉
点赞 评论 收藏
分享
你是来当牛马的吧:行了,照片拍完了,让大家都回工位吧
点赞 评论 收藏
分享
过往烟沉:我说什么来着,java就业面就是广!
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务