Python爬取豆瓣榜单Top250


import pprint
import pandas as pd
import requests
from bs4 import BeautifulSoup
import openpyxl



def download_all_html():
    """
    下载所有页面的HTML
    :return:
    """

    htmls = []
    for num in range(0, 250, 25):
        url = f"https://movie.douban.com/top250?start={num}"
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"
        }
        response = requests.get(url, headers=headers)
        print(response.status_code)
        if response.status_code != 200:
            raise Exception("error")
        htmls.append(response.text)
    return htmls

htmls = download_all_html()
print(htmls)

def parse_single_html(html):
    """
    解析单个HTML,得到数据
    :param html:
    :return:
    """

    soup = BeautifulSoup(html, 'html.parser')
    article_items = (
        soup.find('div', class_='article')
            .find('ol', class_='grid_view')
            .find_all('div', class_='item')
    )
    datas = []
    for article_item in article_items:
        rank = article_item.find('div', class_='pic').find('em').get_text()
        info = article_item.find('div', class_='info')
        title = info.find('div', class_='hd').find('span', class_='title').get_text()
        stars = (
            info.find('div', class_='bd')
            .find('div', class_='star')
            .find_all('span')
        )
        rating_star = stars[0]['class'][0]
        rating_num = stars[1].get_text()
        comments = stars[3].get_text()

        datas.append({
            "rank": rank,
            "title": title,
            "rating_star": rating_star.replace("rating", "").replace("-t", ""),
            "rating_num": rating_num,
            "comments": comments.replace("人评价", "")
        })

    return datas

all_datas = []

for html in htmls:
    all_datas.extend(parse_single_html(html))

pprint.pprint(all_datas)

# 结果存入excel
df = pd.DataFrame(all_datas)
df.to_excel("豆瓣电影Top250.xlsx")





  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
虫(Web Crawler)是一种自动化程序,用于从互联网上收集信息。其主要功能是访问网页、提数据并存储,以便后续分析或展示。虫通常由搜索引擎、数据挖掘工具、监测系统等应用于网络数据抓的场景。 虫的工作流程包括以下几个关键步骤: URL收集: 虫从一个或多个初始URL开始,递归或迭代地发现新的URL,构建一个URL队列。这些URL可以通过链接分析、站点地图、搜索引擎等方式获。 请求网页: 虫使用HTTP或其他协议向目标URL发起请求,获网页的HTML内容。这通常通过HTTP请求库实现,如Python中的Requests库。 解析内容: 虫对获的HTML进行解析,提有用的信息。常用的解析工具有正则表达式、XPath、Beautiful Soup等。这些工具帮助虫定位和提目标数据,如文本、图片、链接等。 数据存储: 虫将提的数据存储到数据库、文件或其他存储介质中,以备后续分析或展示。常用的存储形式包括关系型数据库、NoSQL数据库、JSON文件等。 遵守规则: 为避免对网站造成过大负担或触发反虫机制,虫需要遵守网站的robots.txt协议,限制访问频率和深度,并模拟人类访问行为,如设置User-Agent。 反虫应对: 由于虫的存在,一些网站采了反虫措施,如验证码、IP封锁等。虫工程师需要设计相应的策略来应对这些挑战。 虫在各个领域都有广泛的应用,包括搜索引擎索引、数据挖掘、价格监测、新闻聚合等。然而,使用虫需要遵守法律和伦理规范,尊重网站的使用政策,并确保对被访问网站的服务器负责。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值