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

Python爬取當(dāng)當(dāng)、京東、亞馬遜圖書信息代碼實(shí)例

 更新時(shí)間:2017年12月09日 11:27:34   作者:吳祥生  
這篇文章主要介紹了Python爬取當(dāng)當(dāng)、京東、亞馬遜圖書信息代碼實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下。

注:1.本程序采用MSSQLserver數(shù)據(jù)庫(kù)存儲(chǔ),請(qǐng)運(yùn)行程序前手動(dòng)修改程序開頭處的數(shù)據(jù)庫(kù)鏈接信息

2.需要bs4、requests、pymssql庫(kù)支持

3.支持多線程

from bs4 import BeautifulSoup 
import re,requests,pymysql,threading,os,traceback 
 
try: 
  conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='book',charset="utf8") 
  cursor = conn.cursor() 
except: 
  print('\n錯(cuò)誤:數(shù)據(jù)庫(kù)連接失敗') 
 
#返回指定頁(yè)面的html信息 
def getHTMLText(url): 
  try: 
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'} 
    r = requests.get(url,headers = headers) 
    r.raise_for_status() 
    r.encoding = r.apparent_encoding 
    return r.text 
  except: 
    return '' 
#返回指定url的Soup對(duì)象 
def getSoupObject(url): 
  try: 
    html = getHTMLText(url) 
    soup = BeautifulSoup(html,'html.parser') 
    return soup 
  except: 
    return '' 
#獲取該關(guān)鍵字在圖書網(wǎng)站上的總頁(yè)數(shù) 
def getPageLength(webSiteName,url): 
  try: 
    soup = getSoupObject(url) 
    if webSiteName == 'DangDang': 
      a = soup('a',{'name':'bottom-page-turn'}) 
      return a[-1].string 
    elif webSiteName == 'Amazon': 
      a = soup('span',{'class':'pagnDisabled'}) 
      return a[-1].string 
  except: 
    print('\n錯(cuò)誤:獲取{}總頁(yè)數(shù)時(shí)出錯(cuò)...'.format(webSiteName)) 
    return -1 
 
class DangDangThread(threading.Thread): 
  def __init__(self,keyword): 
    threading.Thread.__init__(self) 
    self.keyword = keyword 
  def run(self): 
    print('\n提示:開始爬取當(dāng)當(dāng)網(wǎng)數(shù)據(jù)...') 
    count = 1 
   
    length = getPageLength('DangDang','http://search.dangdang.com/?key={}'.format(self.keyword))#總頁(yè)數(shù) 
    tableName = 'db_{}_dangdang'.format(self.keyword) 
 
    try: 
      print('\n提示:正在創(chuàng)建DangDang表...') 
      cursor.execute('create table {} (id int ,title text,prNow text,prPre text,link text)'.format(tableName)) 
      print('\n提示:開始爬取當(dāng)當(dāng)網(wǎng)頁(yè)面...') 
      for i in range(1,int(length)): 
        url = 'http://search.dangdang.com/?key={}&page_index={}'.format(self.keyword,i) 
        soup = getSoupObject(url) 
        lis = soup('li',{'class':re.compile(r'line'),'id':re.compile(r'p')}) 
        for li in lis: 
          a = li.find_all('a',{'name':'itemlist-title','dd_name':'單品標(biāo)題'}) 
          pn = li.find_all('span',{'class': 'search_now_price'}) 
          pp = li.find_all('span',{'class': 'search_pre_price'}) 
 
          if not len(a) == 0: 
            link = a[0].attrs['href'] 
            title = a[0].attrs['title'].strip() 
          else: 
            link = 'NULL' 
            title = 'NULL' 
 
          if not len(pn) == 0: 
            prNow = pn[0].string 
          else: 
            prNow = 'NULL' 
 
          if not len(pp) == 0: 
            prPre = pp[0].string 
          else: 
            prPre = 'NULL' 
          sql = "insert into {} (id,title,prNow,prPre,link) values ({},'{}','{}','{}','{}')".format(tableName,count,title,prNow,prPre,link) 
          cursor.execute(sql) 
          print('\r提示:正在存入當(dāng)當(dāng)數(shù)據(jù),當(dāng)前處理id:{}'.format(count),end='') 
          count += 1 
          conn.commit() 
    except: 
      pass 

class AmazonThread(threading.Thread): 
  def __init__(self,keyword): 
    threading.Thread.__init__(self) 
    self.keyword = keyword 
 
  def run(self): 
    print('\n提示:開始爬取亞馬遜數(shù)據(jù)...') 
    count = 1 
    length = getPageLength('Amazon','https://www.amazon.cn/s/keywords={}'.format(self.keyword))#總頁(yè)數(shù) 
    tableName = 'db_{}_amazon'.format(self.keyword) 
     
    try: 
      print('\n提示:正在創(chuàng)建Amazon表...') 
      cursor.execute('create table {} (id int ,title text,prNow text,link text)'.format(tableName)) 
   
      print('\n提示:開始爬取亞馬遜頁(yè)面...') 
      for i in range(1,int(length)): 
        url = 'https://www.amazon.cn/s/keywords={}&page={}'.format(self.keyword,i) 
        soup = getSoupObject(url) 
        lis = soup('li',{'id':re.compile(r'result_')}) 
        for li in lis: 
          a = li.find_all('a',{'class':'a-link-normal s-access-detail-page a-text-normal'}) 
          pn = li.find_all('span',{'class': 'a-size-base a-color-price s-price a-text-bold'}) 
          if not len(a) == 0: 
            link = a[0].attrs['href'] 
            title = a[0].attrs['title'].strip() 
          else: 
            link = 'NULL' 
            title = 'NULL' 
 
          if not len(pn) == 0: 
            prNow = pn[0].string 
          else: 
            prNow = 'NULL' 
 
          sql = "insert into {} (id,title,prNow,link) values ({},'{}','{}','{}')".format(tableName,count,title,prNow,link) 
          cursor.execute(sql) 
          print('\r提示:正在存入亞馬遜數(shù)據(jù),當(dāng)前處理id:{}'.format(count),end='') 
          count += 1 
          conn.commit() 
    except: 
      pass 

class JDThread(threading.Thread): 
  def __init__(self,keyword): 
    threading.Thread.__init__(self) 
    self.keyword = keyword 
  def run(self): 
    print('\n提示:開始爬取京東數(shù)據(jù)...') 
    count = 1 
 
    tableName = 'db_{}_jd'.format(self.keyword) 
     
    try: 
      print('\n提示:正在創(chuàng)建JD表...') 
      cursor.execute('create table {} (id int,title text,prNow text,link text)'.format(tableName)) 
      print('\n提示:開始爬取京東頁(yè)面...') 
      for i in range(1,100): 
        url = 'https://search.jd.com/Search?keyword={}&page={}'.format(self.keyword,i) 
        soup = getSoupObject(url) 
        lis = soup('li',{'class':'gl-item'}) 
        for li in lis: 
          a = li.find_all('div',{'class':'p-name'}) 
          pn = li.find_all('div',{'class': 'p-price'})[0].find_all('i') 
 
          if not len(a) == 0: 
            link = 'http:' + a[0].find_all('a')[0].attrs['href'] 
            title = a[0].find_all('em')[0].get_text() 
          else: 
            link = 'NULL' 
            title = 'NULL' 
           
          if(len(link) > 128): 
            link = 'TooLong' 
 
          if not len(pn) == 0: 
            prNow = '¥'+ pn[0].string 
          else: 
            prNow = 'NULL' 
          sql = "insert into {} (id,title,prNow,link) values ({},'{}','{}','{}')".format(tableName,count,title,prNow,link) 
          cursor.execute(sql) 
          print('\r提示:正在存入京東網(wǎng)數(shù)據(jù),當(dāng)前處理id:{}'.format(count),end='') 
          count += 1 
          conn.commit() 
    except : 
      pass 
def closeDB(): 
  global conn,cursor 
  conn.close() 
  cursor.close() 
 
def main(): 
  print('提示:使用本程序,請(qǐng)手動(dòng)創(chuàng)建空數(shù)據(jù)庫(kù):Book,并修改本程序開頭的數(shù)據(jù)庫(kù)連接語(yǔ)句') 
  keyword = input("\n提示:請(qǐng)輸入要爬取的關(guān)鍵字:") 
 
  dangdangThread = DangDangThread(keyword) 
  amazonThread = AmazonThread(keyword) 
  jdThread = JDThread(keyword) 
   dangdangThread.start() 
  amazonThread.start() 
  jdThread.start() 
  dangdangThread.join() 
  amazonThread.join() 
  jdThread.join() 
   closeDB() 
   print('\n爬取已經(jīng)結(jié)束,即將關(guān)閉....') 
  os.system('pause') 
   
main() 

示例截圖:

關(guān)鍵詞:Android下的部分運(yùn)行結(jié)果(以導(dǎo)出至Excel)

總結(jié)

以上就是本文關(guān)于Python爬取當(dāng)當(dāng)、京東、亞馬遜圖書信息代碼實(shí)例的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:

python爬取亞馬遜書籍信息代碼分享

Python爬蟲實(shí)例爬取網(wǎng)站搞笑段子

Python探索之爬取電商售賣信息代碼示例

如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

相關(guān)文章

  • python redis 批量設(shè)置過期key過程解析

    python redis 批量設(shè)置過期key過程解析

    這篇文章主要介紹了python redis 批量設(shè)置過期key過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Python中的exec、eval使用實(shí)例

    Python中的exec、eval使用實(shí)例

    這篇文章主要介紹了Python中的exec、eval使用實(shí)例,本文以簡(jiǎn)潔的方式總結(jié)了Python中的exec、eval作用,并給出實(shí)例,需要的朋友可以參考下
    2014-09-09
  • Python實(shí)現(xiàn)視頻目標(biāo)檢測(cè)與軌跡跟蹤流程詳解

    Python實(shí)現(xiàn)視頻目標(biāo)檢測(cè)與軌跡跟蹤流程詳解

    通過閱讀相關(guān)文獻(xiàn)及測(cè)試,找到了一種基于多模板匹配的改進(jìn)方法,可以對(duì)遙感視頻衛(wèi)星中的移動(dòng)目標(biāo)進(jìn)行探測(cè),并繪制其軌跡。根據(jù)實(shí)驗(yàn)結(jié)果發(fā)現(xiàn),可以比較有效的對(duì)運(yùn)動(dòng)目標(biāo)進(jìn)行跟蹤
    2023-01-01
  • python指定寫入文件時(shí)的編碼格式方法

    python指定寫入文件時(shí)的編碼格式方法

    今天小編就為大家分享一篇python指定寫入文件時(shí)的編碼格式方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2018-06-06
  • Python+OpenCV讓電腦幫你玩微信跳一跳

    Python+OpenCV讓電腦幫你玩微信跳一跳

    這篇文章主要為大家詳細(xì)介紹了Python+OpenCV讓電腦幫你玩微信跳一跳,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Python求一批字符串的最長(zhǎng)公共前綴算法示例

    Python求一批字符串的最長(zhǎng)公共前綴算法示例

    這篇文章主要介紹了Python求一批字符串的最長(zhǎng)公共前綴算法,涉及Python針對(duì)字符串的遍歷、判斷、計(jì)算等相關(guān)操作技巧,需要的朋友可以參考下
    2019-03-03
  • Python 使用list和tuple+條件判斷詳解

    Python 使用list和tuple+條件判斷詳解

    這篇文章主要介紹了Python 使用list和tuple+條件判斷詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • 使用Pytorch搭建模型的步驟

    使用Pytorch搭建模型的步驟

    這篇文章主要介紹了使用Pytorch搭建模型的步驟,幫助大家更好的理解和使用Pytorch,進(jìn)行機(jī)器學(xué)習(xí),感興趣的朋友可以了解下
    2020-11-11
  • 提升Python編程水平必不可少的重構(gòu)技巧

    提升Python編程水平必不可少的重構(gòu)技巧

    在Python中,編寫可讀性強(qiáng)且Pythonic的代碼是至關(guān)重要的,重構(gòu)技巧是指通過調(diào)整代碼結(jié)構(gòu)和風(fēng)格,使其更符合Python的慣例和標(biāo)準(zhǔn),從而提高代碼的可讀性、簡(jiǎn)潔性和可維護(hù)性,本文將深入探討八項(xiàng)重構(gòu)技巧,幫助您編寫更Pythonic的代碼
    2024-01-01
  • pyinstaller將python程序打包為可執(zhí)行文件

    pyinstaller將python程序打包為可執(zhí)行文件

    這篇文章主要介紹了pyinstaller將python程序打包為可執(zhí)行文件,pyinstaller是一個(gè)python打包工具,它將python程序及所需依賴都打包成一個(gè)可執(zhí)行文件
    2022-08-08

最新評(píng)論

塔城市| 龙游县| 炉霍县| 石狮市| 霞浦县| 吐鲁番市| 江安县| 平潭县| 西吉县| 惠水县| 晋中市| 高雄市| 始兴县| 海城市| 莱西市| 松潘县| 吉隆县| 福泉市| 佛学| 沛县| 常德市| 老河口市| 宁化县| 辽源市| 竹山县| 庆城县| 南皮县| 股票| 旅游| 林芝县| 施甸县| 资中县| 香格里拉县| 太湖县| 明星| 萨迦县| 平武县| 象山县| 金塔县| 巴林右旗| 涿州市|