最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python如何抓取天貓商品詳細(xì)信息及交易記錄

 更新時(shí)間:2018年02月23日 15:08:55   作者:lwhusted  
這篇文章主要為大家詳細(xì)介紹了Python如何抓取天貓商品詳細(xì)信息及交易記錄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Python抓取天貓商品詳細(xì)信息及交易記錄的具體代碼,供大家參考,具體內(nèi)容如下

一、搭建Python環(huán)境

本帖使用的是Python 2.7
涉及到的模塊:spynner, scrapy, bs4, pymmssql

二、要獲取的天貓數(shù)據(jù)

三、數(shù)據(jù)抓取流程

四、源代碼

#coding:utf-8
import spynner
from scrapy.selector import Selector
from bs4 import BeautifulSoup
import random
import pymssql


#------------------------接數(shù)據(jù)庫(kù)-----------------------------#
server="localhost"
user="sa"
password = "123456"
conn=pymssql.connect(server,user,password,"TmallData")
if conn:
  print "DataBase connecting successfully!"
else:
  print "DataBase connecting error!"
cursor=conn.cursor()
#----------------------定義網(wǎng)頁(yè)操作函數(shù)--------------------------#
def py_click_element(browser,pos):
  #點(diǎn)擊網(wǎng)頁(yè)中的元素
  #pos example:'a[href="#description" rel="external nofollow" rel="external nofollow" ]'
  browser.click(pos)
  browser.wait(random.randint(3,10))
  return browser

def py_click_xpath(browser,xpath):
  xpath=xpath+'/@href'
  inner_href=Selector(text=browser.html).xpath(xpath).extract()
  pos='a[href="'+str(inner_href[0])+'" rel="external nofollow" ]'
  browser=py_click_element(browser, pos)
  return browser

def py_webpage_load(browser,url):
  browser.load(url,load_timeout=60)
  browser.wait(10)
  return browser

def py_check_element(browser,xpath):
  #按照xpath查找元素,如果存在則返回True,否則返回False
  if Selector(text=browser.html).xpath(xpath).extract()!=[]:
    return True
  else:
    return False

def py_extract_xpath(browser,xpath):
  if py_check_element(browser, xpath):
    return Selector(text=browser.html).xpath(xpath).extract()[0]
  else:
    return "none"

def py_extract_xpaths(browser,xpaths):
  #批量提取網(wǎng)頁(yè)內(nèi)容
  length=len(xpaths)
  results=[0]*length
  for i in range(length):
    results[i]=py_extract_xpath(browser, xpaths[i])
  return results

#-----------------------------數(shù)據(jù)庫(kù)操作函數(shù)---------------------------#


#-----------------------------數(shù)據(jù)提取函數(shù)----------------------------#
def py_getDealReord(doc):
  soup=BeautifulSoup(doc,'lxml')
  tr=soup.find_all('tr')
  total_dealRecord=[([0]*5)for i in range(len(tr))] 
  i=-1
  for this_tr in tr:
    i=i+1
    td_user=this_tr.find_all('td',attrs={'class':"cell-align-l buyer"})
    for this_td in td_user:
      total_dealRecord[i][0]=this_td.getText().strip(' ')
      #print username
    td_style=this_tr.find_all('td',attrs={'class':"cell-align-l style"})
    for this_td in td_style:
      total_dealRecord[i][1]=this_td.getText(',').strip(' ')
      #print style
    td_quantity=this_tr.find_all('td',attrs={'class':"quantity"})
    for this_td in td_quantity:
      total_dealRecord[i][2]=this_td.getText().strip(' ')
      #print quantity
    td_dealtime=this_tr.find_all('td',attrs={'class':"dealtime"})
    for this_td in td_dealtime:
      total_dealRecord[i][3]=this_td.find('p',attrs={'class':"date"}).getText()
      total_dealRecord[i][4]=this_td.find('p',attrs={'class':"time"}).getText()
  return total_dealRecord
#--------------------獲取要抓取的所有商品鏈接-----------------------#
cursor.execute("""
select * from ProductURLs where BrandName='NB'
""")


file=open("H:\\Eclipse\\TmallCrawling\\HTMLParse\\errLog.txt")
InProductInfo=cursor.fetchall()
browser=spynner.Browser()
for temp_InProductInfo in InProductInfo:

  url='https:'+temp_InProductInfo[2]

  BrandName=temp_InProductInfo[0]
  ProductType=temp_InProductInfo[1]
  print BrandName,'\t',ProductType,'\t',url
  #url= 'https://detail.tmall.com/item.htm?id=524425656711&rn=77636d6db8dea5e30060976fdaf9768d&abbucket=19' 

  try:
    browser=py_webpage_load(browser, url)
  except:
    print "Loading webpage failed."
    file.write(url)
    file.write('\n')
    continue

  xpaths=['//*[@id="J_PromoPrice"]/dd/div/span/text()',\
    '//*[@id="J_StrPriceModBox"]/dd/span/text()',\
    '//*[@id="J_DetailMeta"]/div[1]/div[1]/div/div[1]/h1/text()',\
    '//*[@id="J_PostageToggleCont"]/p/span/text()',\
    '//*[@id="J_EmStock"]/text()',\
    '//*[@id="J_CollectCount"]/text()',\
    '//*[@id="J_ItemRates"]/div/span[2]/text()',\
    '//*[@id="J_DetailMeta"]/div[1]/div[1]/div/ul/li[1]/div/span[2]/text()']
  out_ProductInfo=py_extract_xpaths(browser,xpaths)
  browser=py_click_element(browser,'a[href="#description" rel="external nofollow" rel="external nofollow" ]')
  ProductProperty=py_extract_xpath(browser, '//*[@id="J_AttrUL"]')
  soup=BeautifulSoup(ProductProperty,'lxml')
  li=soup.find_all('li')
  prop=''
  for this_li in li:
    prop=prop+this_li.getText()+'\\'
  prop=prop[0:len(prop)-1]
  out_ProductProperty=prop
  print out_ProductProperty
  cursor.execute("""
  Insert into py_ProductInfo values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
  """,(BrandName,ProductType,url,\
     out_ProductInfo[2],out_ProductInfo[1],\
     out_ProductInfo[0],out_ProductInfo[7],\
     out_ProductInfo[1],out_ProductInfo[3],\
     out_ProductInfo[4],out_ProductInfo[5],\
     out_ProductProperty))
  conn.commit()
  Deal_PageCount=0
  browser=py_click_element(browser, 'a[href="#J_DealRecord" rel="external nofollow" ]')
  #browser.browse(True)
  DealRecord=py_extract_xpath(browser, '//*[@id="J_showBuyerList"]/table/tbody')
  out_DealRecord=py_getDealReord(DealRecord)
  for temp_DealRecord in out_DealRecord:
    if str(temp_DealRecord[4])=='0':
      continue
    cursor.execute("""
    Insert into DealRecord values(%s,%s,%s,%s,%s,%s)
    """,(url,temp_DealRecord[0],temp_DealRecord[1],\
       temp_DealRecord[2],temp_DealRecord[3],\
       temp_DealRecord[4]))
    conn.commit()
  Deal_PageCount=Deal_PageCount+1
  print "Page ",Deal_PageCount
  for i in range(6):
    if (i==0) or (i==2):
      continue
    xpath='//*[@id="J_showBuyerList"]/div/div/a['+str(i)+']'
    if py_check_element(browser,xpath):
      browser=py_click_xpath(browser, xpath)
      DealRecord=py_extract_xpath(browser, '//*[@id="J_showBuyerList"]/table/tbody')
      out_DealRecord=py_getDealReord(DealRecord)
      for temp_DealRecord in out_DealRecord:
        if str(temp_DealRecord[4])=='0':
          continue
        cursor.execute("""
        Insert into DealRecord values(%s,%s,%s,%s,%s,%s)
        """,(url,temp_DealRecord[0],temp_DealRecord[1],\
           temp_DealRecord[2],temp_DealRecord[3],\
           temp_DealRecord[4]))
        conn.commit()
      Deal_PageCount=Deal_PageCount+1
      print "Page ",Deal_PageCount
  while py_check_element(browser, '//*[@id="J_showBuyerList"]/div/div/a[6]'):
    browser=py_click_xpath(browser, '//*[@id="J_showBuyerList"]/div/div/a[6]')
    DealRecord=py_extract_xpath(browser, '//*[@id="J_showBuyerList"]/table/tbody')
    out_DealRecord=py_getDealReord(DealRecord)
    for temp_DealRecord in out_DealRecord:
      if str(temp_DealRecord[4])=='0':
        continue
      cursor.execute("""
      Insert into DealRecord values(%s,%s,%s,%s,%s,%s)
      """,(url,temp_DealRecord[0],temp_DealRecord[1],\
         temp_DealRecord[2],temp_DealRecord[3],\
         temp_DealRecord[4]))
      conn.commit()
    Deal_PageCount=Deal_PageCount+1
    print "Page ",Deal_PageCount

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python實(shí)現(xiàn)自動(dòng)登錄百度空間的方法

    Python實(shí)現(xiàn)自動(dòng)登錄百度空間的方法

    這篇文章主要介紹了Python實(shí)現(xiàn)自動(dòng)登錄百度空間的方法,涉及Python的http請(qǐng)求發(fā)送、獲取響應(yīng)、cookie操作等相關(guān)技巧,需要的朋友可以參考下
    2017-06-06
  • Python多進(jìn)程方式抓取基金網(wǎng)站內(nèi)容的方法分析

    Python多進(jìn)程方式抓取基金網(wǎng)站內(nèi)容的方法分析

    這篇文章主要介紹了Python多進(jìn)程方式抓取基金網(wǎng)站內(nèi)容的方法,結(jié)合實(shí)例形式分析了Python多進(jìn)程抓取網(wǎng)站內(nèi)容相關(guān)實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下
    2019-06-06
  • Python正則表達(dá)式匹配中文用法示例

    Python正則表達(dá)式匹配中文用法示例

    這篇文章主要介紹了Python正則表達(dá)式匹配中文用法,結(jié)合實(shí)例形式分析了Python針對(duì)中文的正則與文件操作相關(guān)技巧,需要的朋友可以參考下
    2017-01-01
  • Python+?Flask實(shí)現(xiàn)Mock?Server詳情

    Python+?Flask實(shí)現(xiàn)Mock?Server詳情

    這篇文章主要介紹了Python+?Flask實(shí)現(xiàn)Mock?Server詳情,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • 淺談Python中的作用域規(guī)則和閉包

    淺談Python中的作用域規(guī)則和閉包

    本文簡(jiǎn)單講解了PYTHON的閉包,作用域的基本知識(shí)配合實(shí)例簡(jiǎn)單明了,適合初學(xué)者
    2018-03-03
  • Python?yield?關(guān)鍵詞,

    Python?yield?關(guān)鍵詞,

    這篇文章主要介紹了Python?yield?關(guān)鍵詞,要理解yield的作用,你必須理解生成器是什么。在理解生成器之前,必須先理解迭代器。下面文章我們就先從
    迭代器開(kāi)始展開(kāi)yield關(guān)鍵詞的相關(guān)自資料 ,需要的朋友可以參考一下
    2021-12-12
  • python繪制簡(jiǎn)單折線圖代碼示例

    python繪制簡(jiǎn)單折線圖代碼示例

    這篇文章主要介紹了python繪制簡(jiǎn)單折線圖代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • Python基于回溯法解決01背包問(wèn)題實(shí)例

    Python基于回溯法解決01背包問(wèn)題實(shí)例

    這篇文章主要介紹了Python基于回溯法解決01背包問(wèn)題,結(jié)合實(shí)例形式分析了Python回溯法采用深度優(yōu)先策略搜索解決01背包問(wèn)題的相關(guān)操作技巧,需要的朋友可以參考下
    2017-12-12
  • python中內(nèi)置函數(shù)range詳解

    python中內(nèi)置函數(shù)range詳解

    Python內(nèi)置函數(shù)range()是一個(gè)用于生成一系列連續(xù)的整數(shù)的函數(shù),它常用于循環(huán)結(jié)構(gòu)中,用于指定循環(huán)的次數(shù)或迭代的范圍,這篇文章主要介紹了python之內(nèi)置函數(shù)range,需要的朋友可以參考下
    2023-07-07
  • Python?threading和Thread模塊及線程的實(shí)現(xiàn)

    Python?threading和Thread模塊及線程的實(shí)現(xiàn)

    這篇文章主要介紹了Python?threading和Thread模塊及線程的實(shí)現(xiàn),Python通過(guò)兩個(gè)標(biāo)準(zhǔn)庫(kù)thread和threading提供對(duì)線程的支持,threading對(duì)thread進(jìn)行了封裝,具體實(shí)現(xiàn)介紹需要的朋友可以參考一下下面文章內(nèi)容
    2022-06-06

最新評(píng)論

奉化市| 闽侯县| 广南县| 茂名市| 滁州市| 贵南县| 昆山市| 铜陵市| 远安县| 沁阳市| 夏河县| 申扎县| 类乌齐县| 西乌珠穆沁旗| 饶阳县| 太谷县| 泰来县| 安新县| 博客| 新平| 仲巴县| 桃江县| 峨眉山市| 巢湖市| 平山县| 高陵县| 建宁县| 琼中| 监利县| 旬邑县| 龙南县| 南开区| 安福县| 攀枝花市| 华宁县| 哈尔滨市| 张家港市| 公安县| 衡阳市| 永昌县| 房产|