将Craigslist中的数据列表导出为CSV

时间:2017-03-14 13:08:41

标签: python csv web

我在Python中运行以下代码以从Craigslist中获取项目列表,但我想将数据导出到CSV文件:

var volumeValue = diskinfo[kDADiskDescriptionVolumeNetworkKey as String]

用于写入csv的代码如下所示,但我总是收到错误:

import csv

from selenium import webdriver
chrome_path=r"C:\Users\z\Desktop\z\Python\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("http://vancouver.craigslist.com")
driver.find_element_by_xpath("""//*[@id="sss0"]/li[19]/a""").click()
posts = driver.find_elements_by_class_name("hdrlnk")
for post in posts:
    print(post.text)

错误是:

with open("C:\Users\z\Desktop\z\Python\test.csv","wb") as output:
    writer = csv.writer(output, lineterminator='\n')
    for post in posts:
        writer.writerows(posts.text)

1 个答案:

答案 0 :(得分:1)

“\ t”将被解释为制表

>>> "C:\Users\z\Desktop\z\Python\test.csv"
'C:\\Users\\z\\Desktop\\z\\Python\test.csv'
>>> print _
C:\Users\z\Desktop\z\Python est.csv
>>> "C:\Users\z\Desktop\z\Python\\test.csv"
'C:\\Users\\z\\Desktop\\z\\Python\\test.csv'
>>> print _
C:\Users\z\Desktop\z\Python\test.csv

以下内容应该有效

with open("C:\Users\z\Desktop\z\Python\\test.csv","wb") as output:

with open(r'C:\Users\z\Desktop\z\Python\test.csv',"wb") as output: