我想一個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我最後一個。
你們能幫我嗎?
非常感謝您的反饋。欣賞它:)現在工作 –