在當當和亞馬遜中搜書并輸出最低價格

這兩天想買幾本關于Python的書,自然是到各網上書店里找,比較哪家最便宜的下手了??墒前l(fā)現很麻煩,需要在每個網站里每本書都要搜一遍,搜完還得計個總價格,看看誰家便宜。所以想到了用Python的爬蟲技術,做一個工具,到各網上書店里找書并計算總價。

京東的搜索很爛,結果是一大堆無關的東西,未找到好方法解決,只好先放棄。目前實現了當當網和亞馬遜搜書并找出最低價和各書的地址,將其保存在results.txt中,并顯示最低總價。

最好是做成一個web頁面,可以接受輸入書名,并且在頁面中直觀的顯示各網站書的總價,還要能一鍵放入購物書。

代碼如下:

# -*- coding:utf-8 -*-
"""在當當和亞馬遜中找書,輸出最低價格"""
import requests, datetime, threading
from urllib.request import quote
from lxml import etree

books = ('流暢的python', 'Python編程快速上手 讓繁瑣工作自動化', '編寫高質量Python代碼的59個有效方法')


def d(book, book_ifos):
    """當當網"""
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3'}
    d_url = 'http://search.dangdang.com/?key={}&act=input&sort_type=sort_xlowprice_asc#J_tab'
    search_url = d_url.format(quote(book, encoding='gbk'))

    r = requests.get(search_url, headers=headers)
    root = etree.HTML(r.text)
    results = root.xpath('//li[starts-with(@class,"line")]')
    """若是有results,則找到書了"""
    if results:
        book_sub = book.lower().split(' ')
        for result in results:
            title = result.xpath('a')[0].attrib['title'].strip()
            """判斷書名中是否含有舊書,有則跳過"""
            if '舊書' in title:
                continue
            """書名按空格折分,并在title中匹配,全匹配才是找對書"""
            hit = False
            for s in book_sub:
                if s in title.lower():
                    hit = True
                else:
                    hit = False
                    continue
            """取得價格和地址,添加到book_ifos中"""
            if hit:
                a = result.xpath('p/span[@class="search_now_price"]')
                if len(a) != 0:
                    price = float(a[0].text[1:])
                else:
                    continue
                url = result.xpath('a')[0].attrib['href']
                book_ifos['dangdang'].append({'title': title, 'price': price, 'url': url})
                break
    else:
        book_ifos['dangdang'].append({'title': book, 'price': 0, 'url': ''})


def z(book, book_ifos):
    """亞馬遜"""
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3'}
    z_url = 'https://www.amazon.cn/s/ref=nb_sb_ss_ime_c_1_5?__mk_zh_CN=%E4%BA%9A%E9%A9%AC%E9%80%8A%E7%BD%91%E7%AB%99&url=search-alias%3Daps&field-keywords={}'

    search_url = z_url.format(book)
    r = requests.get(search_url, headers=headers)
    r.encoding = 'utf-8'
    root = etree.HTML(r.text)
    results = root.xpath('//li[starts-with(@id,"result_")]')
    """若是有results,則找到書了"""
    if results:
        book_sub = book.lower().split(' ')
        book_ifo = []
        for result in results:
            a = result.xpath('div/div/div/a/h2/..')[0]
            title = a.attrib['title'].strip()
            """書名按空格折分,并在title中匹配,全匹配才是找對書"""
            hit = False
            for s in book_sub:
                if s in title.lower():
                    hit = True
                else:
                    hit = False
                    continue
            if hit:
                price_str = result.xpath('div/div/a/span')[1].text
                """
                獲取到的價格為:¥222.222,所以只提取數字部分,并轉為float
                若是電子書,則取不到價格,跳過
                """
                if price_str:
                    price = float(price_str[1:])
                else:
                    continue
                url = a.attrib['href']
                """獲取最低價格"""
                if len(book_ifo) == 0:
                    book_ifo = [title, price, url]
                elif book_ifo[1] > price:
                    book_ifo = [title, price, url]
        book_ifos['Amazon'].append({'title': book_ifo[0], 'price': book_ifo[1], 'url': book_ifo[2]})
    else:
        book_ifos['Amazon'].append({'title': book, 'price': 0, 'url': ''})


if __name__ == '__main__':
    start_time = datetime.datetime.now()
    book_ifos = {'dangdang': [], 'Amazon': []}
    threads = []

    for book in books:
        """當當網價格查詢"""
        t = threading.Thread(target=d, args=(book, book_ifos))
        t.start()
        threads.append(t)
        """亞馬遜網價格查詢"""
        t = threading.Thread(target=z, args=(book, book_ifos))
        t.start()
        threads.append(t)
    """等待線程運行結束"""
    for t in threads:
        t.join()
    """統(tǒng)計各網站的總價格"""
    for site in book_ifos:
        total_price = 0.0
        for book in book_ifos[site]:
            total_price += book['price']
        print(site, '\t', round(total_price, 2))

    print('spend time:', str(datetime.datetime.now() - start_time)[:10])
    with open('results.txt', 'w') as f:
        f.write(str(book_ifos))

如果本文對您有幫助,請給我留個言。謝謝!

?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 1、通過CocoaPods安裝項目名稱項目信息 AFNetworking網絡請求組件 FMDB本地數據庫組件 SD...
    陽明AI閱讀 16,196評論 3 119
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,941評論 25 709
  • 用兩張圖告訴你,為什么你的 App 會卡頓? - Android - 掘金 Cover 有什么料? 從這篇文章中你...
    hw1212閱讀 13,953評論 2 59
  • 幸福 眾所向 讓自己的孩子好好學 學習好 更是家長所期待的 好學的孩子是內動力的 換言之 學習好的孩子 都是主動學...
    一輩子一件事美樂田園閱讀 608評論 0 4
  • 陽春的百花再艷美,卻不及你的瞬間回眸; 初夏的密葉再濃綠,卻不及你的素顏淺笑; 金秋的高風再清爽,卻不及你的言語干...
    落人唯緣閱讀 116評論 0 0

友情鏈接更多精彩內容