1 Star 2 Fork 1

wn/基于flask的购物平台api开发

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
grub.py 2.40 KB
一键复制 编辑 原始数据 按行查看 历史
18573742902 提交于 2022-09-06 22:30 . 爬虫finish
"""
@Project:shoppingapi
@File:grub
@Author:Pengxb
@Date:2022/8/30
"""
import requests
from lxml import html
from urllib.parse import quote # 编码转换函数
def change_name(name): # 编码转换
cname = quote(name, encoding="utf-8")
return cname
# 手机经过utf-8编码为:%E6%89%8B%E6%9C%BA
def get_allmessage(names): #
cname = change_name(names)
url = "https://search.jd.com/Search?keyword=" + cname + "&enc=utf-8"
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"}
response = requests.get(url, headers=header)
response.encoding = "utf-8"
res = html.fromstring(response.text)
shangpin_list = res.xpath('//ul[@class="gl-warp clearfix"]/li')
print(len(shangpin_list))
get_message(shangpin_list)
def get_message(shangpin_list): # 数据筛选
message_dict = []
for message in shangpin_list:
name = message.xpath('div[@class="gl-i-wrap"]/div[@class="p-name p-name-type-2"]/a/em/text()')
# print(name[0])
price = message.xpath('div[@class="gl-i-wrap"]/div[@class="p-price"]/strong/i/text()')
if price == []:
price = ["预约中"]
# print(price[0])
shopname = message.xpath('div[@class="gl-i-wrap"]/div[@class="p-shop"]/span/a/@title')
# print(shopname[0])
href = message.xpath('div[@class="gl-i-wrap"]/div[@class="p-name p-name-type-2"]/a/@href')
# print(href[0])
if shopname == []:
message_dict.append({
"name": name[0],
"price": price[0],
"shopname": '京东官方旗舰店',
"href": href[0]
})
else:
message_dict.append({
"name": name[0],
"price": price[0],
"shopname": shopname[0],
"href": "https:" + href[0]
})
save_message(message_dict)
def save_message(message_dict): # 数据的存储
for message in message_dict:
with open("{name}.txt".format(name="京东商品"), "a", encoding="utf-8") as f:
f.write("价格:" + message["price"] + " 链接:" + message["href"] + " 店铺名称:" + message["shopname"] + " 商品标题:" +
message["name"] + "\n")
if __name__ == "__main__":
name = input("请输入商品名称:")
get_allmessage(name)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/q1583771831/shoppingapi.git
git@gitee.com:q1583771831/shoppingapi.git
q1583771831
shoppingapi
基于flask的购物平台api开发
master

搜索帮助