百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

python爬取京东商品信息(GUI版本)

off999 2024-09-26 16:16 34 浏览 0 评论

前言

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。

作者:DYblog

PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取


python学习交流群,点击链接即可扫码加入



下面直接上代码,看不懂的可以看这篇文章或者注释来学习

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import requests
from bs4 import BeautifulSoup
#from openpyxl import *#写入表格使用,写入txt时报错
import time
from tkinter import * 
import tkinter.messagebox
from  tkinter import ttk
########import结束############

#----------全局变量-----------
https = 'https:'
headers = {
    "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.16 Safari/537.36",
}#定义头部信息,防止被网站阻止
name = []
price = []
introduct = []
urlss = []
#----------全局变量结束-------

#===============函数区域==============
#--------图形界面函数开始--------
def genxin():
	top = Tk()
	top.title("'%s'在'京东'中查询结果"%E1.get())
	top.geometry("800x600+600+100")
	columns = ("物品名", "价格", "简介", "链接")
	treeview = ttk.Treeview(top, show="headings", columns=columns, height='100')
	 
	treeview.column("物品名", width=200, anchor='center')
	treeview.column("价格", width=50, anchor='center')
	treeview.column("简介", width=200, anchor='center')
	treeview.column("链接", width=50, anchor='center')
	
	treeview.heading("物品名", text="物品名")
	treeview.heading("价格", text="价格")
	treeview.heading("简介", text="简介")
	treeview.heading("链接", text="链接")
	treeview.pack()
	print(name)
	print(price)
	print(introduct)
	print(urlss)
	for write_ in range(min(len(name),len(price),len(introduct),len(urlss))): # 写入数据
	    treeview.insert('', write_, values=(name[write_], price[write_], introduct[write_], urlss[write_]))
	top.mainloop()
#--------图形界面函数结束--------
					
def searchstart():#打开页面查找,获取html
	url='https://search.jd.com/Search?keyword='+E1.get()
	url = str(url)
	html = requests.get(url,headers=headers).text#打开链接,获取html
	soup = BeautifulSoup(html, 'html.parser')
	for div in soup.find_all('div',class_="ml-wrap"):#包含价格,销量,商品,页数
		for shangpin in div.find_all('div',class_="goods-list-v2 gl-type-1 J-goods-list"):
			for prices in shangpin.find_all('div',class_="p-price"):#商品价格
				for pricess in prices.find_all('i'):
					if pricess=='':
						pricess='无'
					price.append(pricess.text)
		for shangpin in div.find_all('div',class_="goods-list-v2 gl-type-1 J-goods-list"):#商品
			for name_ in shangpin.find_all('div',class_="p-name p-name-type-2"):
				for titlename in name_.find_all('em'):#简介
					if titlename=='':
						titlename='无'
					introduct.append(titlename.text)
			for name_ in shangpin.find_all('div',class_="p-name p-name-type-2"):
				for introduction in name_.find_all('a',target="_blank"):#商品名
					introduction = introduction.get('title')
					if introduction=='':
						introduction='无'
					name.append(introduction)
			for url in shangpin.find_all('div',class_="p-name p-name-type-2"):
				for urls in url.find_all('a'):
					urlss.append(https+urls['href'])
	print(introduct)
	print(name)
	genxin()
#===============函数区域结束==========


##########图形界面开始#########
root = Tk()
root.title('京东商品 查询')
root.geometry('250x160')
L1 = Label(root, text="商品名: ")
L1.place(x = 5,y = 15)
E1 = Entry(root, bd =2)
E1.place(x = 60,y = 15)

A = Button(root, text ="确定",font=('Arial', 12), width=10, height=1,command=searchstart)
A.place(x = 350,y = 10)#确定按钮

root.mainloop()

运行截图:

相关推荐

python入门到脱坑经典案例—清空列表

在Python中,清空列表是一个基础但重要的操作。clear()方法是最直接的方式,但还有其他方法也可以实现相同效果。以下是详细说明:1.使用clear()方法(Python3.3+推荐)...

python中元组,列表,字典,集合删除项目方式的归纳

九三,君子终日乾乾,夕惕若,厉无咎。在使用python过程中会经常遇到这四种集合数据类型,今天就对这四种集合数据类型中删除项目的操作做个总结性的归纳。列表(List)是一种有序和可更改的集合。允许重复...

Linux 下海量文件删除方法效率对比,最慢的竟然是 rm

Linux下海量文件删除方法效率对比,本次参赛选手一共6位,分别是:rm、find、findwithdelete、rsync、Python、Perl.首先建立50万个文件$testfor...

数据结构与算法——链式存储(链表)的插入及删除,

持续分享嵌入式技术,操作系统,算法,c语言/python等,欢迎小友关注支持上篇文章我们讲述了链表的基本概念及一些查找遍历的方法,本篇我们主要将一下链表的插入删除操作,以及采用堆栈方式如何创建链表。链...

Python自动化:openpyxl写入数据,插入删除行列等基础操作

importopenpyxlwb=openpyxl.load_workbook("example1.xlsx")sh=wb['Sheet1']写入数据#...

在Linux下软件的安装与卸载(linux里的程序的安装与卸载命令)

通过apt安装/协助软件apt是AdvancedPackagingTool,是Linux下的一款安装包管理工具可以在终端中方便的安装/卸载/更新软件包命令使用格式:安装软件:sudoapt...

Python 批量卸载关联包 pip-autoremove

pip工具在安装扩展包的时候会自动安装依赖的关联包,但是卸载时只删除单个包,无法卸载关联的包。pip-autoremove就是为了解决卸载关联包的问题。安装方法通过下面的命令安装:pipinsta...

用Python在Word文档中插入和删除文本框

在当今自动化办公需求日益增长的背景下,通过编程手段动态管理Word文档中的文本框元素已成为提升工作效率的关键技术路径。文本框作为文档排版中灵活的内容容器,既能承载多模态信息(如文字、图像),又可实现独...

Python 从列表中删除值的多种实用方法详解

#Python从列表中删除值的多种实用方法详解在Python编程中,列表(List)是一种常用的数据结构,具有动态可变的特性。当我们需要从列表中删除元素时,根据不同的场景(如按值删除、按索引删除、...

Python 中的前缀删除操作全指南(python删除前导0)

1.字符串前缀删除1.1使用内置方法Python提供了几种内置方法来处理字符串前缀的删除:#1.使用removeprefix()方法(Python3.9+)text="...

每天学点Python知识:如何删除空白

在Python中,删除空白可以分为几种不同的情况,常见的是针对字符串或列表中空白字符的处理。一、删除字符串中的空白1.删除字符串两端的空白(空格、\t、\n等)使用.strip()方法:s...

Linux系统自带Python2&yum的卸载及重装

写在前面事情的起因是我昨天在测试Linux安装Python3的shell脚本时,需要卸载Python3重新安装一遍。但是通过如下命令卸载python3时,少写了个3,不小心将系统自带的python2也...

如何使用Python将多个excel文件数据快速汇总?

在数据分析和处理的过程中,Excel文件是我们经常会遇到的数据格式之一。本文将通过一个具体的示例,展示如何使用Python和Pandas库来读取、合并和处理多个Excel文件的数据,并最终生成一个包含...

【第三弹】用Python实现Excel的vlookup功能

今天继续用pandas实现Excel的vlookup功能,假设我们的2个表长成这样:我们希望把Sheet2的部门匹在Sheet1的最后一列。话不多说,先上代码:importpandasaspd...

python中pandas读取excel单列及连续多列数据

案例:想获取test.xls中C列、H列以后(当H列后列数未知时)的所有数据。importpandasaspdfile_name=r'D:\test.xls'#表格绝对...

取消回复欢迎 发表评论: