×

jsonpath解析写python美女图片爬虫程序

0x_www 0x_www 发表于2021-02-06 13:26:43 浏览305 评论0

抢沙发发表评论

jsonpath解析写python美女图片爬虫程序


JsonPath是一种简单的方法来提取给定JSON文档的部分内容。 JsonPath有许多编程语言,如Javascript,Python和PHP,Java。

JsonPath提供的json解析非常强大,它提供了类似正则表达式的语法,基本上可以满足所有你想要获得的json内容。下面我把官网介绍的每个表达式用代码实现,可以更直观的知道该怎么用它。


需要导入的模块

urllib.parse
requests
json
jsonpath


定义一个url

https://www.duitang.com/search/?kw=%E6%97%B6%E5%B0%9A&type=feed

输入时尚


发现可以达到,发现


Content-Type:

application/json;charset=utf-8


url选择谷歌F12的XHR模式,通过抓到审查的地址

也可以使用ELEMENTS功能定位到图片地址

爱奇艺20210206132900.png


捕获.PNG



发现地址为:https://www.duitang.com/napi/blog/list/by_search/?kw=%E6%97%B6%E5%B0%9A&type=feed&include_fields=top_comments%2Cis_root%2Csource_link%2Citem%2Cbuyable%2Croot_id%2Cstatus%2Clike_count%2Clike_id%2Csender%2Calbum%2Creply_count%2Cfavorite_blog_id&_type=&start=24&_=1612586437204


 定义到url:

https://www.duitang.com/napi/blog/list/by_search/?kw=%E6%97%B6%E5%B0%9A&type=feed&start=24

使用jsonpath解析方法访问url,

image = jsonpath.jsonpath()


利用for循环和with语句生成写入到文件

这里的path是图片img url的地址json属性

========================================================================


import urllib.parse
import requests
import json
import jsonpath


##Content-Type: application/json;charset=UTF-8   JSON解析
#json接口

#https://www.duitang.com/napi/blog/list/by_search/?kw=%E6%97%B6%E5%B0%9A&type=feed&include_fields=top_comments%2Cis_root%2Csource_link%2
# Citem%2Cbuyable%2Croot_id%2Cstatus%2Clike_count%2Clike_id%2Csender%2Calbum%2Creply_count%2Cfavorite_blog_id&_type=&start=24&_=1612586437204
url="https://www.duitang.com/napi/blog/list/by_search/?kw=美女&type=feed&start=24"


ver=requests.get(url).text  #get访问url
html = json.loads(ver)  #json载入
image = jsonpath.jsonpath(html,'$..path')   #JsonPath解析
print(image) #打印解析结果

int=0
for i in image:
    down = requests.get(i)
    with open(r'D:\image\{}.jpg'.format(int), 'wb')as f:  #写入
        f.write(down.content)
        int+= 1
        print("python程序正在下载中,时尚图片", "第",int,"张"+"下载完成")
       


爱奇艺20210206130346.png       
        
   

爱奇艺20210206130417.png