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

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)
相关推荐
青青子衿_213 小时前
TikTok爬取——视频、元数据、一级评论
爬虫·python·selenium
interception6 小时前
爬虫js逆向,jsdom补环境,抖音,a_bogus
javascript·爬虫·python
q***25119 小时前
Python中的简单爬虫
爬虫·python·信息可视化
Glommer1 天前
简单聊一下 tls 指纹校验
爬虫·浏览器
xinxinhenmeihao1 天前
爬虫为什么要用动态ip?动态IP在爬虫中起到哪些作用?
爬虫·网络协议·tcp/ip
APIshop1 天前
代码解析:通过第三方爬虫获取1688商品详情接口
爬虫·okhttp
深蓝电商API2 天前
初级爬虫反爬应对:解决 403、IP 限制的简单方法
爬虫·python
深蓝电商API2 天前
爬虫速度优化:初级阶段如何提升爬取效率(无复杂操作)
爬虫
芝麻开门-新起点2 天前
贝壳的反爬虫机制深度解析
爬虫
q***T5832 天前
MySQL爬虫
数据库·爬虫·mysql