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

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)
相关推荐
q567315236 分钟前
使用reqwest+select实现简单网页爬虫
开发语言·爬虫·golang·kotlin
NEUMaple21 小时前
python爬虫(三)----Selenium
爬虫·python·selenium
华科云商xiao徐1 天前
Node.js浏览器引擎+Python大脑的智能爬虫系统
爬虫·python·node.js
Gloria_niki2 天前
爬虫与数据分析结合案例学习总结
爬虫·学习·数据分析
Ai财富密码2 天前
Python 爬虫:Selenium 自动化控制(Headless 模式 / 无痕浏览)
爬虫·python·selenium
华科云商xiao徐2 天前
异步并发×编译性能:Dart爬虫的实战突围
爬虫·数据可视化·dart
华科云商xiao徐2 天前
使用reqwest+select实现简单网页爬虫
爬虫
华科云商xiao徐2 天前
TypeScript在异步处理与类型安全的双重优势
爬虫·数据挖掘·数据分析
incidite2 天前
爬虫与数据分析入门:从中国大学排名爬取到数据可视化全流程
爬虫·信息可视化·数据分析
华科云商xiao徐3 天前
响应式爬虫系统设计:Scala异步任务编排与弹性容错机制
爬虫·scala