### 页面解析
使用`beautifulsoup`解析页面,获取`JS`中所需数据:
results = soup.find_all(‘script’)[1]
为了能够使用`re`解析获取内容,需要将内容转换为字符串:
image_dirty = str(results)
接下来构造正则表达式获取图片地址:
pattern = re.compile(item, re.I|re.M)
然后查找所有的图片地址:
result_list = pattern.findall(image_dirty)
为了方便获取所需字段,构造解析函数
def analysis(item,results):
pattern = re.compile(item, re.I|re.M)
result_list = pattern.findall(results)
return result_list
打印获取的图片地址:
urls = analysis(r’“path”:“(.*?)”', image_dirty)
urls[0:1]
发现一堆奇怪的字符:
‘images\u002Fresource\u002F2021\u002F06\u002F20\u002F906h89635p0.jpg’,
这是由于网页编码的原因造成的,由于一开始使用`utf-8`方式解码网页,并不能解码`Unicode`:
response.encoding = ‘utf-8’
response.raise_for_status()
soup = BeautifulSoup(response.text, ‘html.parser’)
因此虽然可以通过以下方式获取原始地址:
url = ‘images\u002Fresource\u002F2021\u002F05\u002F22\u002F90h013034p0.jpg’
decodeunichars = url.encode(‘utf-8’).decode(‘unicode-escape’)
但是我们可以通过`response.encoding = 'unicode-escape'`进行更简单的解码,缺点是网页的许多中文字符会变成乱码,但是字不重要不是么?看图!
![图片](https://img-blog.csdnimg.cn/20210707104529848.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xPVkVteTEzNDYxMQ==,size_16,color_FFFFFF,t_70#pic_center)
#### 创建图片保存路径
为了下载图片,首先创建图片保存路径:
创建图片保存路径
if not os.path.exists(webp_file):
os.makedirs(webp_file, exist_ok=True)
if not os.path.exists(png_file):
os.makedirs(png_file, exist_ok=True)
#### 图片下载
当我们使用`另存为`选项时,发现格式为`webp`,但是上述获取的图片地址为`jpg`或`png`,如果直接存储为`jpg`或`png`格式,会导致格式错误。
![图片格式](https://img-blog.csdnimg.cn/20210707104731803.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xPVkVteTEzNDYxMQ==,size_16,color_FFFFFF,t_70#pic_center)因此需要重新构建`webp`格式的文件名:
name = img.split(‘/’)[-1]
name = name.split(‘.’)[0]
name_webp = name + ‘.webp’
由于获取的图片地址并不完整,需要添加网站主页来构建图片地址:
from urllib.request import urljoin
domain = ‘https://img2.huashi6.com’
img_url = urljoin(domain,img)
接下来就是下载图片了:
r = requests.get(img_url,headers=headers)
if r.status_code == 200:
with open(name_webp, ‘wb’) as f:
f.write(r.content)
#### 格式转换
最后,由于得到的图片是`webp`格式的,如果希望得到更加常见的`png`格式,需要使用`PIL`库进行转换:
image_wepb = Image.open(name_webp)
image_wepb.save(name_png)
#### 爬取结果展示
![爬取结果](https://img-blog.csdnimg.cn/20210707105342875.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xPVkVteTEzNDYxMQ==,size_16,color_FFFFFF,t_70#pic_center)
### 完整程序
import time
import requests
from bs4 import BeautifulSoup
import os
import re
from urllib.request import urljoin
from PIL import Image
webp_file = ‘girlfriends_webp’
png_file = ‘girlfriends_png’
print(os.getcwd())
创建图片保存路径
if not os.path.exists(webp_file):
os.makedirs(webp_file, exist_ok=True)
if not os.path.exists(png_file):
os.makedirs(png_file, exist_ok=True)
headers = {
‘User-Agent’:‘Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:86.0) Gecko/20100101 Firefox/86.0’,
#‘Cookie’:‘’
‘Connection’: ‘keep-alive’
}
url_pattern = “https://www.huashi6.com/tags/161?p={}”
domain = ‘https://img2.huashi6.com’
图片地址获取函数
def analysis(item,results):
pattern = re.compile(item, re.I|re.M)
result_list = pattern.findall(results)
return result_list
图片格式转换函数
def change_webp2png(name_webp, name_png, img_url):
try:
image_wepb = Image.open(name_webp)
image_wepb.save(name_png)
except:
download_image(name_webp, name_png, img_url)
图片下载函数
def download_image(name_webp, name_png, img_url):
if not os.path.exists(name_png):
if os.path.exists(name_webp):
os.remove(name_webp)
print(img_url)
一、Python所有方向的学习路线
Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照下面的知识点去找对应的学习资源,保证自己学得较为全面。
二、Python必备开发工具
工具都帮大家整理好了,安装就可直接上手!
三、最新Python学习笔记
当我学到一定基础,有自己的理解能力的时候,会去阅读一些前辈整理的书籍或者手写的笔记资料,这些笔记详细记载了他们对一些技术点的理解,这些理解是比较独到,可以学到不一样的思路。
四、Python视频合集
观看全面零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。
五、实战案例
纸上得来终觉浅,要学会跟着视频一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
六、面试宝典
简历模板![在这里插入图片描述](https://img-blog.csdnimg.cn/646863996ac44da8af500c049bb72fbd.png#pic_center)
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!