网页抓取数据 免费(Python学习资料的小伙伴素材图片及图片来源于网络,仅供学习)

优采云 发布时间: 2022-04-13 10:21

  网页抓取数据 免费(Python学习资料的小伙伴素材图片及图片来源于网络,仅供学习)

  前言

  本文文字及图片来源于网络,仅供学习交流,不具有任何商业用途。如有任何问题,请及时联系我们进行处理。

  PS:如需Python学习资料,可点击下方链接自行获取

  Python免费学习资料及*敏*感*词*流答案点击加入

  相关环境配置

  相关模块可以通过pip安装

  确定着陆页

  

  如果要爬取这些素材图片,必须先从头开始,并且需要知道素材下载的地址是什么。

  

  

  点击素材进入素材详情页面,可以看到本地下载地址,复制多几个素材的下载地址链接:

  http://www.aiimg.com/sucai.php?open=1&aid=126632&uhash=70a6d2ffc358f79d9cf71392

http://www.aiimg.com/sucai.php?open=1&aid=126630&uhash=99b07c347dc24533ccc1c144

http://www.aiimg.com/sucai.php?open=1&aid=126634&uhash=d7e8f7f02f57568e280190b4

123

  每个环节的帮助是不同的。这应该是每种材料的 ID。背后的uhash是什么?

  本来想着网页数据里有没有接口数据直接找这个参数,但是在开发者工具里搜索没有这个参数,看看网页源码里有没有这个下载链接~

  

  如果有这个链接,我们拿到链接后就可以直接下载了~

  普通*敏*感*词*:

  1.打开开发者工具,查看网页是否返回你想要获取的数据。

  

  

  可以发现我们需要的数据在网页的label中,请求网页获取返回的数据

  import requests

url = 'http://www.aiimg.com/list.php?tid=1&ext=0&free=2&TotalResult=5853&PageNo=1'

headers = {

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'

}

response = requests.get(url=url, headers=headers)

print(response.text)

1234567

  

  解析网络数据

  import parsel

selector = parsel.Selector(response.text)

lis = selector.css('.imglist_d ul li a::attr(href)').getall()

for li in lis:

num_id = li.replace('.html', '').split('/')[-1]

new_url = 'http://www.aiimg.com/sucai.php?aid={}'.format(num_id)

response_2 = requests.get(url=new_url, headers=headers)

selector_2 = parsel.Selector(response_2.text)

data_url = selector_2.css('.downlist a.down1::attr(href)').get()

title = selector_2.css('.toart a::text').get()

download_url = 'http://www.aiimg.com' + data_url

1234567891011

  

  保存数据

  资料均为psd、ai或cdr文件,保存后为zip压缩包形式

  def download(url, title):

path = '路径' + title + '.zip'

response = requests.get(url=url, headers=headers)

with open(path, mode='wb') as f:

f.write(response.content)

print('{}已经下载完成'.format(title))

123456

  

  

  

  这只是单页爬取,也可以实现多页爬取~

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线