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

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)
相关推荐
嘘嘘出差10 分钟前
爬虫中级——滑块验证码的应对方法
爬虫
明月_清风15 小时前
robots.txt 完全指南:从入门到精通
后端·爬虫
明月_清风15 小时前
从零写一个"像人"的爬虫:反爬攻防实战指南
后端·爬虫
嘘嘘出差2 天前
从零到精通:爬虫技术进阶路线全解析
爬虫
嘘嘘出差2 天前
Python 爬虫入门实战:爬取豆瓣电影 Top 250
开发语言·爬虫·python
嘘嘘出差2 天前
天气数据监控系统
jvm·爬虫
小花皮猪3 天前
Bright Data CLI 入门教程:命令行实现 Web Scraping 与结构化数据采集
爬虫
Maiko Star3 天前
Python项目实战——网络机器人(爬虫)
爬虫·python·机器人
STDD3 天前
n8n 实战工作流模板:GitHub→飞书通知、定时爬虫→数据库、Webhook→多渠道推送
爬虫·github·飞书
跨境数据猎手4 天前
反向海淘实战|独立站搭建+国内电商API对接
开发语言·爬虫·架构