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

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)
相关推荐
进击的雷神6 小时前
网站 SEO 功能怎么测:从手工检查到自动化脚本的实战思路
运维·爬虫·自动化·官网seo·收录
深蓝电商API9 小时前
TLS 指纹为什么越来越重要?
爬虫·tls 指纹
2601_9622035110 小时前
小白爬虫——selenium入门超详细教程
爬虫·selenium·测试工具
IT毕设实战小研2 天前
电影数据可视化推荐系统
大数据·后端·爬虫·python·算法·信息可视化·课程设计
笔触狂放2 天前
第3章 抓取静态网页数据
爬虫·python
笔触狂放2 天前
第1章 认识网络爬虫
爬虫
深蓝电商API3 天前
Referer 校验原理
爬虫·referer
天空1103 天前
你的网站在 AI 眼里是什么样:三类「人能看见、AI 看不见」的内容
爬虫·seo
zx_741484813 天前
【Python入门】爬虫实战:Requests + XPath 从基础到实战
开发语言·爬虫·python
for_ever_love__3 天前
python爬虫: GET请求与POST请求
开发语言·爬虫·python·网络编程