各位大大,我嘗試做一個比價的程式,但是我嘗試讓使用者可以重複輸入想要的書名,搜尋完後可以存在booklist裡,但是booklist都會複寫新的值,舊值都不見了,有沒有辦法可以讓booklist一直增加新的數值,最後再存入?
以下是我的代碼:
`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("網路抓取資料")
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`
booklist=[]
把这个放到外面