鱼C论坛

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

爬虫怎么保存到指定的文件夹?

[复制链接]
发表于 2023-12-5 21:01:56 | 显示全部楼层 |阅读模式

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

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

x
import requests
from lxml import etree

url = 'https://ssr1.scrape.center/'

headers={
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36'}
name = requests.get(url=url,headers=headers)

n = name.text

n1 = etree.HTML(n)
t = n1.xpath('//div[@id="index"]//a/img/@src')
n = n1.xpath('//div[@id="index"]//a/h2/text()')

for i in range(len(t)):
    url=t[i]
    n1=n[i]
    # print(url)
    img = requests.get(url=url,headers=headers)
    with open(n1+'.jpg','wb') as f:  #这里怎么保存到指定的文件夹中,比如 :  d:/图片/爬虫图片
        f.write(img.content)
      
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-24 11:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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