爬取优美图库里的照片,并存到文件夹中

爬取图片


前言

这篇博客里面主要写的就是爬取图片的实例,可能比较简单,大佬请指正啊,如果有不对的话


提示:以下是本篇文章正文内容,下面案例可供参考

一、代码如下

#https://www.umei.cc/bizhitupian/huyanbizhi/2.htm
#https://www.umei.cc/bizhitupian/huyanbizhi/13.htm

import requests
from bs4 import BeautifulSoup
import os
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.47'
}
urls = ['https://www.umei.cc/bizhitupian/huyanbizhi/{}.htm'.format(i) for i in range(2,13)]
urls=urls[0]
num = 0
for url in urls:
    def get_main(url):
         global num
         response = requests.get(url,headers=headers)
         response.encoding = 'utf-8'
         main_page = BeautifulSoup(response.text,"html.parser")
         alst = main_page.find("div",attrs={"class":"TypeList"}).find_all("a",attrs={"class":"TypeBigPics"})

         n=1
         for a in alst:
            href = a.get("href")
            response1 = requests.get(href)
            response1.encoding = 'utf-8'
            child_page = BeautifulSoup(response1.text,"html.parser")
            src = child_page.find("div",attrs={"class":"ImageBody"}).find("img").get("src")
            n+=1
    
            print('正在下载第'+str(url)+'中的第'+str(num+1)+'张' )
            try:
                if src is not None:
                    pic = requests.get(src,timeout=7)
                else:
                    continue
            except BaseException:
                    print('错误')
            else:
                string = file+r'\\'+str(num+1)+'jpg'
                fp= open(string,'wb')
                fp.write(pic.content)
                fp.close()
                num=num+1


if __name__ == '__main__':
    file= input('请输入你想建立的文件夹名称')
    y=os.path.exists(file)
    if y==1:
        print('请重新输入新的文件夹')
        file = input('新文件夹名称')
        os.mkdir(file)
    else:
        os.mkdir(file)
    get_main(url)
        

二、使用步骤

1.引入库

代码如下(示例):

import requests
from bs4 import BeautifulSoup
import os

2.读入数据

其实大致思路和上一篇的相似,因为我比较喜欢用beautiful soup解析网页,其实是不太会正则,而且这种方法挺方便的

  • 特别点就是用了建文件夹的方法,上篇里面没有怎么好好讲述,就是这样新建文件夹,其实可以提前定义好文件夹的名字的

file= input(‘请输入你想建立的文件夹名称’)
y=os.path.exists(file)
if y==1:
print(‘请重新输入新的文件夹’)
file = input(‘新文件夹名称’)
os.mkdir(file)
else:
os.mkdir(file)

  • 还有一点就是这篇文章里面引用了函数

if name == ‘main’:

get_main(url)

总结

提示:这里对文章进行总结:
以上就是今天要讲的内容,本文仅仅简单介绍了爬取图片的使用,写的也比较清楚的吧,也比较简单,希望有不对的地方,请大佬指正

好的,这是一个简单的示例代码,可以爬取汽车之家某个车型的口碑数据,并将其存储到Excel文件: ```python import requests from bs4 import BeautifulSoup import openpyxl # 汽车之家口碑网址 url = 'https://k.autohome.com.cn/spec/{}/index_{}.html' # 车型ID和总页数 car_id = '3395' # 以奔驰GLA为例 total_pages = 3 # 爬取前3页数据 # 创建Excel文件 wb = openpyxl.Workbook() ws = wb.active ws.append(['用户', '购买地点', '购买车款', '购买年份', '评分', '优点', '缺点']) # 循环爬取每一页的数据 for page in range(1, total_pages+1): # 发送请求获取页面内容 response = requests.get(url.format(car_id, page)) soup = BeautifulSoup(response.text, 'html.parser') # 解析页面内容,获取口碑信息 comments = soup.find_all('div', {'class': 'mouthcon'}) for comment in comments: user = comment.find('div', {'class': 'choose-con'}).text.strip() buy_place = comment.find('div', {'class': 'mouth-item choose'}).find_all('span')[1].text.strip() buy_model = comment.find('div', {'class': 'mouth-item choose'}).find_all('span')[3].text.strip() buy_year = comment.find('div', {'class': 'mouth-item choose'}).find_all('span')[5].text.strip() score = comment.find('div', {'class': 'score-con'}).find('span', {'class': 'score-number'}).text.strip() pros = comment.find('div', {'class': 'text-con'}).find('div', {'class': 'text-cont text-cont1'}).text.strip() cons = comment.find('div', {'class': 'text-con'}).find('div', {'class': 'text-cont text-cont2'}).text.strip() # 将口碑信息写入Excel文件 ws.append([user, buy_place, buy_model, buy_year, score, pros, cons]) # 保存Excel文件 wb.save('car_comments.xlsx') ``` 这个示例代码使用requests库和BeautifulSoup库来爬取汽车之家某个车型的前3页口碑数据,并将其存储到Excel文件。你可以根据自己的需要修改车型ID和总页数,以及Excel文件的文件名和路径。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值