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

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 小时前
爬虫框架:Feapder使用心得
爬虫·python
Pyeako12 小时前
python爬虫--selenium库和requests库
爬虫·python·requests库·selenium库
小白学大数据12 小时前
Python爬虫实战:抓取《疯狂动物城》相关社交媒体讨论
开发语言·爬虫·python·媒体
B站计算机毕业设计之家16 小时前
基于python京东商品销售数据分析可视化系统 Django框架 爬虫 大数据(源码)
大数据·爬虫·python·selenium·机器学习·数据分析·django
xinxinhenmeihao16 小时前
爬虫采集中怎么删除重复的代理ip地址?
爬虫·网络协议·tcp/ip
深蓝电商API16 小时前
爬虫请求伪装成手机App:User-Agent + Header + TLS指纹
爬虫·智能手机
jinxinyuuuus17 小时前
抖音在线去水印:HTTP/2流量分析、反爬虫的对称与非对称加密
爬虫·网络协议·http
Pyeako1 天前
python网络爬虫
开发语言·爬虫·python·requsets库
小白学大数据1 天前
基于文本检测的 Python 爬虫弹窗图片定位与拖动实现
开发语言·爬虫·python
不叫猫先生1 天前
AI Prompt 直达生产级爬虫,Bright Data AI Scraper Studio 让数据抓取更高效
人工智能·爬虫·prompt