5.Python实现爬取某瓣电影排行榜

Python是十分擅长开发爬虫程序的,今天我给大家带来一个小工具,可以快速爬取某瓣电影

编写代码

下面是一个简单的Python脚本,可以快速爬取某瓣电影 TOP250 相关信息:

import requests
import re


def top250_crawer(url, sum):
    headers = {
        'User-agent''Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/1'
    }
    response = requests.get(url, headers=headers)
    title = re.findall('<span class="title">(.*?)</span>', response.text, re.S)
    new_title = []
    for t in title:
        if '&nbsp;/&nbsp' not in t:
            new_title.append(t)
    data = re.findall('<br>(.*?)</p>', response.text, re.S)
    time = []
    country = []
    for str1 in data:
        str1 = str1.replace(' ''')
        str1 = str1.replace('n''')
        time_data = str1.split('&nbsp;/&nbsp;')[0]
        country_data = str1.split('&nbsp;/&nbsp;')[1]
        time.append(time_data)
        country.append(country_data)
    for j in range(len(country)):
        sum += 1
        print(str(sum) + '.' + new_title[j] + ',' + country[j] + ',' + time[j])


url = 'https://movie.douban.com/top250'
sum = 0
'遍历10页数据,250条结果'
for a in range(10):
    if sum == 0:
        top250_crawer(url, sum)
        sum += 25
    else:
        page = '?start=' + str(sum) + '&filter='
        new_url = url + page
        top250_crawer(new_url, sum)
        sum += 25

运行代码

然后运行该程序,它将会爬取某瓣电影相关信息,并输出到控制台。

结论

通过本文的介绍,你学会了如何使用Python简单爬取某瓣电影的信息。


原文始发于微信公众号(学长工具库):5.Python实现爬取某瓣电影排行榜

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/267407.html

(0)
李, 若俞的头像李, 若俞

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!