使用python简单的爬取页面的图片和标题

import requests
from bs4 import BeautifulSoup
from PIL import Image
import os
from io import BytesIO
import time

url = "http://www.huajiao.com/category/1000"
#写入你需要爬取的网页
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"}

r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.content, 'html.parser')
items = soup.find_all('img', class_='img')
#image的类
name = list(soup.find_all('p', class_='name fl'))
#名字的类
a=[]
for names in name:
   names=names.text
   a.append(names)




folder_path = './photo'
if os.path.exists(folder_path) == False:
   os.makedirs(folder_path)

for index, item in enumerate(items):
     if item:
         html = requests.get(item.get('src'))
         img_name =str( a[ (index + 1)]).strip() + '.png'
         image = Image.open(BytesIO(html.content))
         image.save('E:\Python\photo/' + img_name)
         print('第%d张图片下载完成' % (index + 1))
         time.sleep(1)  # 自定义延时
print('抓取完成')
运行示意图

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值