Python爬取案例之猫眼电影排行榜TOP100

爬取网站:https://maoyan.com/board/4

话不多说,上代码
import requests
import random
from bs4 import BeautifulSoup
import os

global lots_headers
lots_headers = [
    "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36",
    "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0",
    "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)",
    'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11',
    'Opera/9.25 (Windows NT 5.1; U; en)',
    'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
    'Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Kubuntu)',
    'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12',
    'Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/1.2.9',
    "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:10.0) Gecko/20100101 Firefox/10.0",
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36'
]
global headers
headers = {'User-Agent': random.choice(lots_headers)}

global url
url = "https://maoyan.com/board/4?offset="

# 保存路径
global savePath
savePath = 'G:\\排行'

global num
num = 1

global testurl


# 保存文件
def savefile(savepath, filename, clean_text, score_text):
    global num
    if os.path.exists(savePath) is False:
        os.mkdir(savePath)
    os.chdir(savePath)
    try:
        f = open(filename, 'a+')
        f.write('NO.'+str(num)+'  '+clean_text+'  评分:'+score_text+'\n')
        f.close()
    except Exception as e:
        print(e)


def main():
    global offset
    offset = 0
    global num
    try:
        while offset < 100:
            testurl = url + str(offset)
            res = requests.get(testurl, headers=headers)
            soup = BeautifulSoup(res.text, 'html.parser')
            name = soup.find_all('div', class_='movie-item-info')
            score = soup.find_all('div', class_='movie-item-number score-num')
            filename = '猫眼电影排行榜Top100.txt'
            global i
            i = 0
            while i <= 9:
                print("第"+str(num)+"名:""    评分:"+score[i].text.replace('\n', ' '))
                clean_text = '  '.join(name[i].text.split())
                print(clean_text)
                # 文件写入
                savefile(savePath, filename, clean_text, score[i].text.replace('\n', ' '))
                num = num + 1
                i = i + 1
            offset = offset + 10
        print('爬取结束')
    except Exception as e:
        print(e)


if __name__ == '__main__':
    main()


此程序简单易懂,有问题请留言。(大佬勿喷,我是个可怜的小白)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值