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

1、爬取豆瓣电影top250

bash 复制代码
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、爬取价格

bash 复制代码
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、爬取书名

bash 复制代码
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)
相关推荐
我可以将你更新哟17 小时前
【爬虫】下载ffmpeg,爬取b站视频,把音频和视频合成一个视频
爬虫·ffmpeg·音视频
胡伯来了17 小时前
08 - 数据收集 - 网页采集工具Selenium
爬虫·python·selenium·rag·网络采集
Cherry的跨界思维1 天前
25、AI时代的数字生存战:爬虫与反爬虫的数据争夺全面解析
人工智能·爬虫·机器学习·python爬虫·python办公自动化·python反爬虫
我可以将你更新哟2 天前
【爬虫】使用协程(asyncio)爬取旁边桌面图片并存入数据
爬虫
我可以将你更新哟2 天前
【爬虫】爬取斗罗大陆漫画,面向对象封装(存入数据库)
数据库·爬虫·python
傻啦嘿哟2 天前
Docker部署Scrapy集群:爬虫容器化实战指南
爬虫·scrapy·docker
小白学大数据2 天前
利用 Selenium 与 BeautifulSoup 构建链家动态爬虫
开发语言·爬虫·selenium·beautifulsoup
李昊哲小课2 天前
简化版天气爬虫教程
爬虫·python
电商API_180079052472 天前
淘宝商品视频提取API全解析:从授权到落地实战
爬虫·python·信息可视化·数据分析·音视频
hugh_oo3 天前
100 天学会爬虫 · Day 16:如何分析登录接口?爬虫视角下的登录流程拆解方法
爬虫