鱼C论坛

 找回密码
 立即注册
查看: 2115|回复: 1

爬虫求助

[复制链接]
发表于 2023-9-13 17:13:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
https://baijiahao.baidu.com/s?id=1765736748275541689

像这种百家号的文章图片怎么爬取
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-9-13 17:13:49 | 显示全部楼层
您好,想要爬取这种类型的文章图片,可以使用Python的第三方库requests和BeautifulSoup来实现。具体步骤如下:

1. 使用requests库获取网页内容:

  1. import requests

  2. url = 'https://baijiahao.baidu.com/s?id=1765736748275541689'
  3. response = requests.get(url)
  4. html = response.text
复制代码


2. 使用BeautifulSoup库解析网页内容,并获取图片链接:

  1. from bs4 import BeautifulSoup

  2. soup = BeautifulSoup(html, 'html.parser')
  3. img_tags = soup.find_all('img')

  4. img_urls = []
  5. for img in img_tags:
  6.     img_url = img.get('src')
  7.     img_urls.append(img_url)
复制代码


3. 下载图片:

  1. import os

  2. save_folder = 'images'
  3. if not os.path.exists(save_folder):
  4.     os.makedirs(save_folder)

  5. for img_url in img_urls:
  6.     img_name = img_url.split('/')[-1]
  7.     img_path = os.path.join(save_folder, img_name)

  8.     with open(img_path, 'wb') as f:
  9.         img_data = requests.get(img_url).content
  10.         f.write(img_data)
复制代码


以上是一个简单的爬虫实现过程,但需要注意的是,爬取他人网站内容需要获得对方的授权,否则可能会侵犯对方的知识产权。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-23 00:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表