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

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)
相关推荐
深蓝电商API18 小时前
Frida入门到实战:hook so层与Java层的姿势总结
爬虫·frida
小静AI工程实验室21 小时前
robots.txt 不是门禁:RFC 9309 规则、缓存与 Python 爬虫的 10 个边界
爬虫·python·缓存
远航计算机1 天前
AI 爬虫分三种,你 robots.txt 里挡的是哪一种?
人工智能·爬虫·aigc
深蓝电商API2 天前
大厂验证码对抗升级:从“破解”到“养环境”的思路转变
爬虫·验证码
Getgit2 天前
亮数据 | Browser API 实战:Booking.com 动态页面抓取对比
爬虫·亮数据·broowser api
深蓝电商API3 天前
reCAPTCHA/hCaptcha/Turnstile三大国际验证码对比研究
爬虫·recaptcha·hcaptcha·turnstile
深蓝电商API3 天前
从零训练自己的验证码识别模型:数据生成到部署
爬虫·验证码
wuyk5554 天前
Python 网络爬虫入门到实战 第 04 章:请求头、UA 伪装、超时、异常处理、基础反爬绕过
开发语言·爬虫·python
weixin_440401694 天前
质朴的爬虫+数据处理
爬虫·python·数据分析·pandas
沙漠之主4 天前
Python 教学设计资料:从入门到实战的完整课程方案
爬虫·python