這兩天想買幾本關于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))
如果本文對您有幫助,請給我留個言。謝謝!