我想知道如何将我的结果从抓取导出到具有不同列的csv文件
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")
我可以将我的数据放入csv文件,但不知道如何为它创建3个专用列。 “标题:”+ Header_final应该是第一列。 “价格:”+ Price_final第二个。和“Deeplink:”+ Deeplink_final我的最后一个。
你可以帮帮我吗?答案 0 :(得分:0)
只需使用csv模块即可。您导入它,但不要使用它。你可以在那里找到文件。
答案 1 :(得分:0)
在for循环之前添加以下内容以创建csv编写器并写入标题行:
writer = csv.writer(output_file)
csv_fields = ['Header', 'Price', 'Deeplink']
if gdata:
writer.writerow(csv_fields)
然后在循环体中,用以下代码替换你的write语句:
writer.writerow([Header_final, Price_final, Deeplink_final])