猫眼电影榜单前50爬取

猫眼电影榜单前50爬取

import requests
import json
from lxml import etree
#for n in range(0,5):
def getOnepage(n):
    url = 'https://maoyan.com/board/4?offset={}'.format(n*10)
    header = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'}
    r = requests.get(url,header)
    return r.text
def parse(text):
    #item = {}
    html = etree.HTML(text)
    k = 0
    names = html.xpath('//div[@class="movie-item-info"]/p[@class="name"]/a/@title')
    stars =  html.xpath('//div[@class="movie-item-info"]/p[@class="star"]/text()')
    relasetimes = html.xpath('//div[@class="movie-item-info"]/p[@class="releasetime"]/text()')
    #scores = html.xpath('//div[@class="movie-item-number score-num"]/p[@class="score"]/i[@class="integer"]/text()')
    for num, name,star,relasetime in zip(nums,names,stars,relasetimes):
        #data = json.dumps(item)
        with open('排行.txt', "a") as f:
            f.write(str(num) + '《'+name+'》' + star +'        ' + relasetime + '\n')
    del nums[k:k+10]
    k = k+1
nums = []
for m in range(1,100):
    nums.append(m)
#print(nums[9])
for i in range(0,5):
    text = getOnepage(i)
    parse(text)

你可能感兴趣的:(猫眼电影榜单前50爬取)