最近写了个专门爬百度的,厥后又想爬京东的,照样接纳上次的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('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() ###########图形界面竣事######### #written by DY #http://dyblog.tk #e-mail:duyun888888@qq.com
运行截图:
如果有不懂的,可以直接留言或者联系duyun888888@qq.com
,欢迎进入欧博开户网址(Allbet Gaming):www.aLLbetgame.us,欧博allbet网址开放欧博allbet网址、会员注册、代理开户、电脑客户端下载、苹果安卓下载等业务。
网友评论
最新评论
欧博电脑版欢迎进入欧博电脑版(Allbet Game):www.aLLbetgame.us,欧博官网是欧博集团的官方网站。欧博官网开放Allbet注册、Allbe代理、Allbet电脑客户端、Allbet手机版下载等业务。谁一起聊聊这个文章
欧博电脑版欢迎进入欧博电脑版(Allbet Game):www.aLLbetgame.us,欧博官网是欧博集团的官方网站。欧博官网开放Allbet注册、Allbe代理、Allbet电脑客户端、Allbet手机版下载等业务。谁一起聊聊这个文章
@环球UG网址 强世功说,是次全国人大常委会决议出来后,反对派造势,引发部门港人泛起一些恐慌情绪,这很大水平是因为人人都陷入恐惧的想像中。他建议,与其恐惧,人人不如去起劲领会真实的情形,研究国家政策,研究内地与香港关系,研究中央推动立法的靠山,系统研究中央对香港政策。只有准确明白中央为什么要推出立法,立法针对谁,又是为了谁,才会有理性的方式,做出准确的决议。 太温柔的文了
大佬们看完了吗?
Telegram中文群组(www.Telegram8.vip)是一个Telegram群组分享平台,飞机群组内容包括Telegram群组索引、Telegram群组导航、新加坡telegram群组、telegram中文群组、telegram群组(其他)、Telegram 美国 群组、telegram群组爬虫、电报群 科学上网、小飞机 怎么 加 群、tg群等内容,为广大电报用户提供各种电报群组/电报频道/电报机器人导航服务。太好看了,看了一天