2015-08-14 44 views
0

我想一個CSV文件,瞭解如何抓取與不同列爬行輸出與colums

import requests 
from bs4 import BeautifulSoup 
import csv 

user_agent = {'User-agent': 'Chrome/43.0.2357.124'} 

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

r = requests.get("http://www.bla/paris/") 
soup = BeautifulSoup(r.content) 

g_data = soup.find_all("div", {"class": "itemsContent clearafter"}) 
for item in g_data: 
    Header = item.find_all("div", {"class": "InnprodInfos"}) 
    Header_final = (Header[0].contents[0].text.strip()) 
    price = item.find_all("div", {"class": "prodPrice"}) 
    Price_final = (price[0].contents[0].text.strip()) 
    Deeplink = item.find_all("a") 
    for t in Deeplink: 
     Deeplink_final = (t.get("href")) 

    print("Header: " + Header_final + " | " + "Price: " + Price_final + " | " + "Deeplink: " + Deeplink_final) 
    output_file.write("Header: " + Header_final + " | " + "Price: " + Price_final + " | " + "Deeplink: " + Deeplink_final + "\n") 

I'm能夠把我的數據爲csv文件,但一個CSV文件導出我的結果不知道如何爲它創建3個專用列。 「Header:」+ Header_final應該是第一列。 「Price:」+ Price_final第二個。和「Deeplink:」+ Deeplink_final我最後一個。

你們能幫我嗎?

回答

0

只需使用csv模塊。您可以導入它,但不要使用它。你可以在那裏找到文件。

0

之前的for循環添加以下創建CSV作家,寫的標題行:在循環體

writer = csv.writer(output_file) 
csv_fields = ['Header', 'Price', 'Deeplink'] 
if gdata: 
    writer.writerow(csv_fields) 

接着,以該更換你寫的語句:

writer.writerow([Header_final, Price_final, Deeplink_final]) 
+0

非常感謝您的反饋。欣賞它:)現在工作 –