当前位置: 首页 > news >正文

爬虫爬取豆瓣电影、价格、书名

1、爬取豆瓣电影top250

import requests
from bs4 import BeautifulSoup

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
}

for i in range(0, 250, 25):
    print(f"--------第{i+1}到{i+25}个电影------------")
    response = requests.get(f"https://movie.douban.com/top250?start={i}", headers=headers)

    if response.ok:
        html = response.text
        soup = BeautifulSoup(html, "html.parser")
        all_titles = soup.findAll("span", attrs={"class": "title"})
        j = i
        for title in all_titles:
            title_string = title.string
            if "/" not in title_string:
                j += 1
                print(f"{j}、{title_string}")
    else:
        print("请求失败")

2、爬取价格

import requests
from bs4 import BeautifulSoup

content = requests.get("http://books.toscrape.com/").text
soup = BeautifulSoup(content, "html.parser")
# 因为价格在标签为p的里面,所以写p,它的属性为class="price_color"
all_prices = soup.findAll("p", attrs={"class": "price_color"})
print(all_prices)
for price in all_prices:
    print(price.string[2:])

3、爬取书名

import requests
from bs4 import BeautifulSoup

content = requests.get("http://books.toscrape.com/").text
soup = BeautifulSoup(content, "html.parser")
# 因为书名在h3中,又包了一层a,所以先找h3,再找a
all_titles = soup.findAll("h3")
for title in all_titles:
    all_links = title.findAll("a")
    for link in all_links:
        print(link.string)

相关文章:

  • 网络安全选择题20道——附答案
  • 前端axios与python库requests的区别
  • VideoPoet: Google的一种用于零样本视频生成的大型语言模型
  • HarmonyOS构建第一个JS应用(FA模型)
  • java实现深度优先搜索 (DFS) 算法
  • html table+css实现可编辑表格
  • 如何使用宝塔面板+Discuz+cpolar内网穿透工具搭建可远程访问论坛服务
  • 写一个java状态模式的详细实例
  • 怎么实现Servlet的自动加载
  • 卸载了Visual Studio后,在vscode中执行npm i或npm i --force时报错,该怎么解决?
  • log4j学习
  • 鸿蒙开发中的坑(持续更新……)
  • cfa一级考生复习经验分享系列(十一)
  • 低时延,可扩展的 l4s 拥塞控制算法
  • MySQL——内置函数
  • 【SpringCloud笔记】(10)消息总线之Bus
  • 分支限界法求解01背包(优先队列)【java】
  • 2023年第七届强网杯初赛 WP
  • Python期末复习第一章——概述
  • vmware安装中标麒麟高级服务器操作系统 V7.0
  • 中国电信财务部总经理周响华调任华润集团总会计师
  • 上海市委政法委召开会议传达学习总书记重要讲话精神
  • 这个部位最容易变老,却被很多姑娘忽视了
  • 缅甸国防军继续延长临时停火期限至5月31日
  • “五一”假期国内出游3.14亿人次,同比增长6.4%
  • 工人日报关注跟着演出去旅游:票根经济新模式兴起,让过路客变过夜客