macos 使用vscode 开发python 爬虫(开发二)

以下是一个示例,用于爬取一个公开的示例网站(http://books.toscrape.com),并提取书籍的标题和价格:

python 复制代码
import requests
from bs4 import BeautifulSoup

# 发起请求并获取网页内容
url = '可以用上面的链接🔗'
response = requests.get(url)
html_content = response.text

# 使用BeautifulSoup解析网页内容
soup = BeautifulSoup(html_content, 'html.parser')

# 提取书籍标题和价格
books = soup.find_all('article', class_='product_pod')

for book in books:
    title = book.h3.a['title']
    price = book.find('p', class_='price_color').text

    print('书名:', title)
    print('价格:', price)
    print('---')

这个示例使用了一个公开的书籍网站(http://books.toscrape.com),通过发送HTTP请求获取网页内容,并使用BeautifulSoup解析网页内容。然后,它找到所有的书籍信息,并提取书名和价格,并将它们打印出来。

运行脚本

powershell 复制代码
python spider.py

请确保你已经安装了所需的Python库(requests和beautifulsoup4)以及它们的依赖项。你可以使用以下命令来安装这些库:

powershell 复制代码
pip install requests beautifulsoup4

如果想要图片

powershell 复制代码
print("Runoob")

import requests
from bs4 import BeautifulSoup

# 发起请求并获取网页内容
url = '可以用上面的链接🔗'
response = requests.get(url)
html_content = response.text

# 使用BeautifulSoup解析网页内容
soup = BeautifulSoup(html_content, 'html.parser')

# 提取书籍标题和价格
books = soup.find_all('article', class_='product_pod')

for book in books:
    img_element = book.find('img', class_='thumbnail')
    img_url = img_element.get('src')
    title = book.h3.a['title']
    price = book.find('p', class_='price_color').text

    print('图片:', img_url)
    print('书名:', title)
    print('价格:', price)
    print('---')
相关推荐
0思必得010 分钟前
[Web自动化] 处理爬虫异常
运维·爬虫·python·selenium·自动化·web自动化
喵手15 分钟前
Python爬虫零基础入门【第九章:实战项目教学·第17节】内容指纹去重:URL 变体/重复正文的识别!
爬虫·python·爬虫实战·python爬虫工程化实战·零基础python爬虫教学·内容指纹去重·url变体
喵手38 分钟前
Python爬虫零基础入门【第五章:数据保存与入库·第1节】先学最通用:CSV/JSONL 保存(可复现、可分享)!
爬虫·python·python爬虫实战·python爬虫工程化实战·python爬虫零基础入门·数据保存与入库·csv/jsonl
子夜江寒1 小时前
OpenCV 学习:图像拼接与答题卡识别的实现
python·opencv·学习·计算机视觉
bjxiaxueliang1 小时前
一文掌握Python Flask:HTTP微服务开发从入门到部署
python·http·flask
SunnyRivers1 小时前
Python 中的 HTTP 客户端:Requests、HTTPX 与 AIOHTTP 对比
python·httpx·requests·aiohttp·区别
u0109272712 小时前
持续集成/持续部署(CI/CD) for Python
jvm·数据库·python
lixin5565562 小时前
基于迁移学习的图像风格增强器
java·人工智能·pytorch·python·深度学习·语言模型
阡陌..2 小时前
浅谈SAR图像处理---形态学滤波
图像处理·人工智能·python
qq_229058013 小时前
python-Dgango项目收集静态文件、构建前端、安装依赖
开发语言·python