各位大大,我嘗試做一個比價的程式,但是我嘗試讓使用者可以重複輸入想要的書名,搜尋完後可以存在booklist裡,但是booklist都會複寫新的值,舊值都不見了,有沒有辦法可以讓booklist一直增加新的數(shù)值,最後再存入?
以下是我的代碼:
`from bs4 import BeautifulSoup
import time
import requests
import csv
URL="https://search.books.com.tw/search/query/key/{0}/cat/all"
import codecs
def generate_search(url,keyword):
url=url.format(keyword)
return url
def generate_resource(url):
headers={"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64)" "AppleWebKit/537.36 (KHTML, like Gecko)" "Chrome/68.0.3440.106" "Safari/537.36"}
return requests.get(url,headers=headers)
def parse_html(r):
if r.status_code==requests.codes.ok:
r.encoding='utf8'
soup=BeautifulSoup(r.text,"lxml")
else:
print("HTTP 請求錯誤..."+url)
soup=None
return soup
def get_ISBN(url):
soup=parse_html(generate_resource(url))
if soup!=None:
gogo=soup.find(itemprop="productID")["content"][5:]
if(gogo!=-1):
return gogo
else:
gogo=None
return gogo
else:
return None
def get_prices(isbn):
price1,price2=None,None
url1="http://www.eslite.com/Search_BW.aspx?query="+isbn
soup=parse_html(generate_resource(url1))
if soup!=None:
price1=soup.find_all("span",class_=["price_sale","特價"])[2].text
else:
price1=None
url2="https://www.kingstone.com.tw/search/result.asp?c_name={0}&se_type=4"
soup=parse_html(generate_resource(url2.format(isbn)))
if soup !=None:
price2=soup.find("span",class_="sale_price").text
if (isbn==None):
price1=None
price2=None
return price1,price2
else:
return price1,price2
def web_scraping_bot(url):
booklist=[]
print("網(wǎng)路抓取資料")
soup=parse_html(generate_resource(url))
if soup!=None:
tag_item=soup.find_all(class_="item")
for item in tag_item:
book=[]
book.append(item.find("img")["alt"])
isbn=get_ISBN("https:"+item.find("a")["href"])
book.append(isbn)
price=item.find(class_="price").find_all("b")
book.append(price[1].string+"元")
price1,price2=get_prices(isbn)
book.append(price1)
book.append(price2)
booklist.append(book)
print("Wait for 2 secs...")
time.sleep(2)
return booklist
data=[["名字","ISBN","博客來","誠品","金石堂"]]
def save_to_csv(booklist,file):
with codecs.open(file,'w+','utf_8_sig') as fp:
writer =csv.writer(fp)
writer.writerows(data)
for book in booklist:
writer.writerow(book)
while True:
name=input("請輸入書名:")
url=generate_search(URL,name)
print(url)
booklist=web_scraping_bot(url)
for item in booklist:
print(item)
save_to_csv(booklist,"booklist6.csv")
print("要再輸入書名嗎?y/n")
y_b=input()
if(y_b=="y"):
continue
else:
break`
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。