Python 官方文档:入门教程 => 点击学习
京东页面分析:点击图片上传按钮,上传一张小图,可以看到上传失败了,不要紧,在network里面可以看到image?op=upload的信息,点开就可以看到图片上传的接口了.接口地址:https://search.jd.com/image?o
京东页面分析:
点击图片上传按钮,上传一张小图,可以看到上传失败了,不要紧,在network里面可以看到
image?op=upload的信息,点开就可以看到图片上传的接口了.
接口地址:https://search.jd.com/image?op=upload
提交post的请求的时候,还需要带上一些headers里面的信息.在接口信息上面都能找到的。
利用requests-html向接口提交post请求,代码如下:
from requests_html import HTMLSession
session = HTMLSession()
post_url = 'Https://search.jd.com/image?op=upload'
headers = {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/WEBp,image/apng,**;q=0.8,application/signed-exchange;v=b3",
"accept-encoding": "gzip, deflate, br",
"accept-language": "zh-CN,zh;q=0.9",
"content-length": "4068000",
"origin": "https://search.jd.com",
"user-agent": "Mozilla/5.0 (windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"
}
files = {'file': open('333.jpg','rb')}
r = session.post(post_url, headers=headers, files=files, timeout=30)
# 利用正则匹配出路径
ret = re.findall('[(]["](.*)["][)]', r.text)[0]
if ret == 'ERROR.UPLOAD_FORMAT':
# 图片识别失败
print(False)
else:
s = session.get("https://search.jd.com/image?path={}&op=search".format(ret))
url_list = s.html.xpath("//div[@class='p-img']/a/@href")[0:3]
for x in url_list:
print("https:" + x)
rr = session.get("https:" + x)
# 拿到商品描述信息
jd_describe = rr.html.xpath("//div[@class='sku-name']/text()")[0].replace(' ', '')
print(jd_describe)
# 拿到商品分类信息
jd_classification_list = []
for y in rr.html.xpath("//div[@class='crumb fl clearfix']//a"):
if y.text.strip() == '':
pass
else:
jd_classification_list.append(y.text.strip())
five = rr.html.xpath("//div[@class='crumb fl clearfix']//div[@class='item ellipsis']/text()")
if five:
jd_classification_list.append(five[0])
print(jd_classification_list)
运行结果:
--结束END--
本文标题: requests-html京东图片上传找电商分类
本文链接: https://lsjlt.com/news/181226.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0