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

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)
相关推荐
Cisyam^10 小时前
Bright Data AI Scraper Studio:一句话生成企业级爬虫
人工智能·爬虫
一招定胜负13 小时前
网络爬虫(第二部)
爬虫
电商API_1800790524714 小时前
Python爬虫从入门到实战:核心技术与合规指南
大数据·数据库·爬虫
sugar椰子皮16 小时前
【爬虫框架-6】中间件的另一种写法实现
爬虫·中间件
三喵22317 小时前
跨域 iframe 内嵌的同源策略适配方案-Youtube举例
前端·爬虫
深蓝电商API17 小时前
Curl_cffi实战:完美伪装成真实浏览器TLS/JA3指纹
chrome·爬虫·反爬
啊巴矲17 小时前
小白从零开始勇闯人工智能:爬虫初级篇(2-网络爬虫(2))
爬虫
深蓝电商API18 小时前
爬虫数据增量更新:时间戳、offset、WebSocket 长连接方案
爬虫
陈老老老板18 小时前
让AI替你写爬虫:基于自然语言的 AI Scraper Studio 实战解析
人工智能·爬虫
sugar椰子皮18 小时前
【爬虫框架-5】实现一下之前的思路
开发语言·爬虫·python