Python爬取淘宝店铺和评论

安装开发需要的一些库

(1) 安装mysql 的驱动:在Windows上按win+r输入cmd打开命令行,输入命令pip install pymysql,回车即可。

(2) 安装自动化测试的驱动selenium:在命令行中输入pip install selenium回车。

(3) 安装标签解析库pyquery: 在命令行中输入pip install pyquery回车。

(4) Anaconda指的是一个开源的Python发行版本,其包含了conda、Python等180多个科学包及其依赖项,下载anaconda,安装后配置环境变量,在path中添加E:\Anaconda3\anaconda\Library\bin,重启电脑使环境变量生效,安装jieba库,在命令行中输入pip install jieba回车。

(5) 下载ChromeDriver,官方网址为:[http://chromedriver.storage.googleapis.com/](http://chromedriver.storage.googleapis.com/)

index.html,并将chromedriver.exe放在Python安装目录的Scripts文件夹下。

 

实现

- 爬取数据的实现主要是用到了Pyquery、selenium库,以下代码主要实现了对淘宝的检索、翻页和对数据的提取。

``` ''' # 设置网站最大响应时间 wait=WebDriverWait(driver,50) class TaoBaoSearch: # 初始化,默认搜索为None,创建数据库连接 def __init__(self,search=None): self.name=search self.mysql=to.Data_oper() # 对淘宝网的搜索 def search(self): # 设置源网站,这里设置淘宝网站为源网站 driver.get("https://www.taobao.com/")#J_TSearchForm > div.search-button > button # “q”为淘宝首页输入框的标签,这里定位到该输入框,并设置要搜索商品的名字 imput=driver.find_element_by_id("q") imput.send_keys(self.name) # wait.until()该方法的作用是加载出来搜索结果总页数之后开始往下

执行 pageText=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"#mainsrp-pager > div > div > div > div.total"))) total=re.search("\d+",pageText.text) # 该方法返回搜索结果的总页数 return total.group(0) # 提取出相应的数据 def parseHtml(self): html=driver.page_source#获取网页源代码 doc=qp(html) # 得到到class为m-itemlist下面的class是.items .item的div iteams=doc(".m-itemlist .items .item").items() # 根据标签选择器提取出需要的数据 for item in iteams: # src=item(".pic .img").attr("src") src=item(".row .J_ClickStat").attr("href") # 该店铺的链接 person=item(".row .deal-cnt").text() #购买该商品的人数 title=item(".row .J_ClickStat").text().split("\n") # 标题 shop=item(".row .shopname").text() # 商品 location=item(".row .location").text() # 地区 # 将提取到的数据放到数组中保存起来 data=[] data.append(str(title[0].strip())) data.append(str(shop.strip())) data.append(str(location.strip())) # 剔除无用字 data.append(str(person[:-3].strip())) data.append(str(src).strip()) # 调用mysql.insert_data()方法将提取到的数据插入到数据库中 self.mysql.insert_data(data) # 对网页进行翻页的方法 def nextpage(self,pagenumber): # 定位到翻页的按钮前的输入框,也就是对其进行跳转 pageInput=driver.find_element_by_css_selector("#mainsrp-pager > div > div > div > div.form > input") pageInput.clear() pageInput.send_keys(pagenumber) # 定位到跳转按钮,对其进行翻页 pageButton=driver.find_element_by_css_selector("#mainsrp-pager > div > div > div > div.form > span.btn.J_Submit") pageButton.click() wait.until(EC.text_to_be_present_in_element((By.CSS_SELECTOR,"#mainsrp-pager > div > div > div > ul > li.item.active > span"),str(pagenumber))) self.parseHtml() # 定义主函数,调用上面的的方法 def main(self): total=int(self.search()) for i in range(2,total): self.nextpage(i) self.mysql.close()

```

以下代码是一个排序的算法,其主要作用是在界面上显示多少行数据,主要思路为:根据用户输入的数字创建一个数组,读取数据库中得到数据,分离出来购买人数并转换成int类型,将数据每次添加一个到数组中,当数组的长度等于用户想要显示最大行数时,对其数组中的数据从大到小进行排序,接下来,每当读取一个数据之后,就对数组中最小的那个进行比较,如果比起小,就跳过,否则,对该数据进行插入操作,并删除之前最小的那个数据,最后数组中保存的就是购买人数最多的前n条数据。

主要代码如下:

'''
遇到不懂的问题?Python学习交流群:219539519满足你的需求,资料都已经上传群文件,可以自行下载!
'''
#对数据进行排序,data为购买人数
def shot_data(self,data,i=10): # i为用户想要显示的最大行数,默认为10行
 top=[]
 if i>len(data):
 i=len(data)
 for x in data:
 if len(top)<i: # 控制数组的长度,另其大小等于i
 top.append(x)
 if len(top)==i:
 top.sort(reverse=True) # 数组内的数据进行排序
 else:
 l=len(top)
 y=len(top)
 t=1
 if x>top[l-1]: # 判断其数值是否大于数组内的最小值
 while x>top[l-t] and y>0: # 控制循环条件
 t+=1
 y-=1
 if y!=0: # y的值若是==0,那么该数值就是最大值
 for c in range(1,t):
 top[l-c]=top[l-c-1]
 top[l-t+1]=x
 else:
 for c in range(1,t):
 top[l-c]=top[l-c-1]
 top[0]=x
 return top # 返回装有最大的前i个数的数组

以下代码是对评论进行关键词的提取,用到了jieba库中的一些方法。

主要代码如下:

def dis_an(self):
# 清空显示界面
 self.txtMess.delete(1.0,END)
 t=to.Data_oper()
# 得到数据库中的存储信息
 test=t.dis_only_discuss()
# 定义字符串adg,v
 adg=""
 v=""
# 对评论进行分割并标注词性
 word=psg.cut(test)
# w为词意,f为词性
 for w,f in word:
 # 判断词性是否为形容词
 if f.startswith('a'):
 print(w)
 adg=adg+","+w
 # 判断词性是否为动词
 elif f.startswith('v'):
 v=v+","+w
 # 根据该词的权重提取出前5个词
tags=jieba.analyse.extract_tags(adg,topK=5)
 tags1=jieba.analyse.extract_tags(v,topK=5)