当前位置: 首页 > 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)
http://www.dtcms.com/a/5772.html

相关文章:

  • 网络安全选择题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
  • 动物分类识别教程+分类释义+界面展示
  • 【排序算法】C语言实现选择排序与冒泡排序
  • OpenEuler安装内网穿透工具实现ssh连接openEuler系统
  • pip install multiprocessing报错子进程错误error: metadata-generation-failed
  • IP代理科普| 共享IP还是独享IP?两者的区别与优势
  • C++哈希表的实现
  • Anylogic Pro 8.8.x for Mac / for Linux Crack
  • DOM and XPATH
  • linux中主从复制的架构和读写分离的方式
  • SpaceDesk如何连接平板/PC(生产力副屏)