最近写了个专门爬百度的,后来又想爬京东的,还是采用上次的BeautifulSoup+requests模块

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

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#written by DY
#http://dyblog.tk
#e-mail:duyun888888@qq.com
########import###############
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('500x600')
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()
###########图形界面结束#########
#written by DY
#http://dyblog.tk
#e-mail:duyun888888@qq.com

如果有不懂的,可以直接留言或者联系duyun888888@qq.com

python爬京东(带GUI)的更多相关文章

  1. python实现的、带GUI界面电影票房数据可视化程序

    代码地址如下:http://www.demodashi.com/demo/14588.html 详细说明: Tushare是一个免费.开源的python财经数据接口包.主要实现对股票等金融数据从数据采 ...

  2. Python 实现的、带GUI界面的词云生成器

    代码地址如下:http://www.demodashi.com/demo/14233.html 详细说明: "词云"就是数据可视化的一种形式,给出一段文本,根据文本中词语的出现频率 ...

  3. python自带gui插件tkinter使用小结

    1.tkinter简介 Tkinter(即 tk interface,简称"Tk")本质上是对 Tcl/Tk 软件包的 Python 接口封装,它是 Python 官方推荐的 GU ...

  4. python爬虫系列之爬京东手机数据

    python抓京东手机数据 作者:vpoet mail:vpoet_sir@163.com #coding=utf-8 import urllib2 from lxml import etree im ...

  5. 用Python爬取分析【某东618】畅销商品销量数据,带你看看大家都喜欢买什么!

    618购物节,辰哥准备分析一波购物节大家都喜欢买什么?本文以某东为例,Python爬取618活动的畅销商品数据,并进行数据清洗,最后以可视化的方式从不同角度去了解畅销商品中,名列前茅的商品是哪些?销售 ...

  6. Python爬取南京市往年天气预报,使用pyecharts进行分析

    上一次分享了使用matplotlib对爬取的豆瓣书籍排行榜进行分析,但是发现python本身自带的这个绘图分析库还是有一些局限,绘图不够美观等,在网上搜索了一波,发现现在有很多的支持python的绘图 ...

  7. Python3.x:Selenium+PhantomJS爬取带Ajax、Js的网页

    Python3.x:Selenium+PhantomJS爬取带Ajax.Js的网页 前言 现在很多网站的都大量使用JavaScript,或者使用了Ajax技术.这样在网页加载完成后,url虽然不改变但 ...

  8. 萌新学习Python爬取B站弹幕+R语言分词demo说明

    代码地址如下:http://www.demodashi.com/demo/11578.html 一.写在前面 之前在简书首页看到了Python爬虫的介绍,于是就想着爬取B站弹幕并绘制词云,因此有了这样 ...

  9. python 爬取王者荣耀高清壁纸

    代码地址如下:http://www.demodashi.com/demo/13104.html 一.前言 打过王者的童鞋一般都会喜欢里边设计出来的英雄吧,特别想把王者荣耀的英雄的高清图片当成电脑桌面 ...

随机推荐

  1. Jmeter让压测随时做起来(转载)

    为什么要压测 这个问题问的其实挺没有必要的,做开发的同学应该都很清楚,压测的必要性,压力测试主要目的就是让我们在上线前能够了解到我们系统的承载能力,和当前.未来系统压力的提升情况,能够评估出当前系统的 ...

  2. window10下通过docker安装swoole,运行laravel-swoole服务

    最近公司压测一个接口,用laravel框架,业务逻辑就是从缓存中取数据,tps总是上不去,于是决定换下swoole来替换php-fpm,来处理php请求,tps比原来高了好几倍. 现在有个问题就是wi ...

  3. 基于springcloud gateway + nacos实现灰度发布(reactive版)

    什么是灰度发布? 灰度发布(又名金丝雀发布)是指在黑与白之间,能够平滑过渡的一种发布方式.在其上可以进行A/B testing,即让一部分用户继续用产品特性A,一部分用户开始用产品特性B,如果用户对B ...

  4. redis 深入理解redis 主从复制原理

    redis 主从复制 master 节点提供数据,也就是写.slave 节点负责读. 不是说master 分支不能读数据,也能只是我们希望将读写进行分离. slave 是不能写数据的,只能处理读请求 ...

  5. MATLAB作图之一

    问题 在使用MATLAB当中的imagesc作图的时候,如果数据矩阵元素太少,得到的图看起来会很"粗糙"(图1).那么如何得到更为"圆润"的图像呢? A = r ...

  6. @codeforces - 575E@ Spectator Riots

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 一个以 (0, 0) 为左下角,(10^5, 10^5) 为右上 ...

  7. VMWare虚拟机开启时显示模块“Disk”启动失败的解决方案

    找到虚拟机所在的目录, 将 .vmx文件打开 将文件vmci0.present = "TRUE"改为 vmci0.present = "FALSE" 删除以.l ...

  8. Spark原始码系列(五)分布式缓存

    问题导读:spark缓存是如何实现的?BlockManager与BlockManagerMaster的关系是什么? 这个persist方法是在RDD里面的,所以我们直接打开RDD这个类. def pe ...

  9. C语言中main函数的参数argc和argv

    参考资料: http://wiki.opencv.org.cn/index.php/Main%E5%87%BD%E6%95%B0%E5%8F%82%E6%95%B0argc%EF%BC%8Cargv% ...

  10. Python itchat.get_chatrooms() 抓取群聊不全的问题

    1 rooms = itchat.get_chatrooms() 2 f = codecs.open("3.txt","w","utf-8" ...