2016-04-20 83 views
0

我想在Excel中將價格和相應地址寫入CSV文件。到目前爲止,我有這個代碼給出了照片中下面顯示的輸出。將美麗的湯輸出寫入CSV

我想要的是一列價格優先和一列地址秒。

[![from bs4 import BeautifulSoup 
import requests 
import csv 


number = "1" 
url = "http://www.trademe.co.nz/browse/categoryattributesearchresults.aspx?cid=5748&search=1&v=list&134=1&nofilters=1&originalsidebar=1&key=1654466070&page=" + number + "&sort_order=prop_default&rptpath=350-5748-3399-" 
r= requests.get(url) 
soup = BeautifulSoup(r.content) 


output_file= open("output.csv","w") 

price = soup.find_all("div",{"class":"property-card-price-container"}) 

address = soup.find_all("div",{"class":"property-card-subtitle"}) 


n = 1 
while n != 150: 
    b = (price\[n\].text) 
    b = str(b) 
    n = n + 1 
    output_file.write(b) 

output_file.close()][1]][1] 

l

回答

3

也許這樣的事情?

from bs4 import BeautifulSoup 
import requests 
import csv 
.... 
r = requests.get(url) 
soup = BeautifulSoup(r.content) 
price = soup.find_all("div",{"class":"property-card-price-container"}) 
address = soup.find_all("div",{"class":"property-card-subtitle"}) 

dataset = [(x.text, y.text) for x,y in zip(price, address)] 

with open("output.csv", "w", newline='') as csvfile: 
    writer = csv.writer(csvfile) 
    for data in dataset[:150]: #truncate to 150 rows 
     writer.writerow(data) 
0

您的代碼有幾個問題。將價格和地址分成單獨的列表會使網站切換項目的順序等,並使它們混淆起來。當刮這樣的條目時,首先找到較大的封閉容器,然後從那裏縮小是很重要的。

很遺憾,您提供的網址已不存在。因此,我剛剛瀏覽了另一套此例的列表:

from bs4 import BeautifulSoup 
import requests 
import csv 

url = 'http://www.trademe.co.nz/property/residential-property-for-sale' 
url += '/waikato/view-list' 

r = requests.get(url) 
soup = BeautifulSoup(r.content, 'html5lib') 

with open('output.csv', 'w', newline='') as csvfile: 

    propertyWriter = csv.writer(csvfile, quoting=csv.QUOTE_ALL) 

    for listing in soup.find_all('div', 
           {'class': 'property-list-view-card'}): 
     price = listing.find_all('div', 
           {'class': 'property-card-price-container'}) 
     address = listing.find_all('div', 
            {'class': 'property-card-subtitle'}) 

     propertyWriter.writerow([price[0].text.strip(), 
           address[0].text.strip()])