尝试抓取以下网站时 csv 文件中的格式化问题
Formatting Issue in csv file while trying to scrape the following website
我正在尝试抓取网站以获取标题和价格,但是一旦数据被提取并保存在 csv 文件中,价格列的格式就会受到干扰,并且无法正确显示在列中,例如8,900 美元在一列中变为 8 美元,900 移至下一列。
from selenium import webdriver
import time
max_pages = 1
driver = webdriver.Chrome()
with open('autotrader.csv', 'w') as f:
f.write("Title,Price \n")
for i in range(1, max_pages + 1):
url = "https://www.autotrader.co.uk/car-search?advertClassification=standard&postcode=WC2N%205DU&onesearchad=Used&onesearchad=Nearly%20New&onesearchad=New&advertising-location=at_cars&is-quick-search=TRUE&include-delivery-option=on&page=" + str(max_pages)
driver.get(url)
title = driver.find_elements_by_xpath('//h3[@class="product-card-details__title"]')
price =driver.find_elements_by_xpath('//div[@class="product-card-pricing__price"]')
page_items = len(title)
with open('autotrader.csv', 'a') as f:
for i in range(page_items):
f.write(title[i].text + "," + price[i].text + "\n")
driver.close()
使用 csv.writer
它将正确引用其中包含分隔符的字段:
import csv
# ... code to fetch titles and prices ...
with open('autotrader.csv', 'w', newline='') as f:
w = csv.writer(f)
w.writerow(['Title','Price'])
for t,p in zip(title,price):
w.writerow([t.text,p.text])
我正在尝试抓取网站以获取标题和价格,但是一旦数据被提取并保存在 csv 文件中,价格列的格式就会受到干扰,并且无法正确显示在列中,例如8,900 美元在一列中变为 8 美元,900 移至下一列。
from selenium import webdriver
import time
max_pages = 1
driver = webdriver.Chrome()
with open('autotrader.csv', 'w') as f:
f.write("Title,Price \n")
for i in range(1, max_pages + 1):
url = "https://www.autotrader.co.uk/car-search?advertClassification=standard&postcode=WC2N%205DU&onesearchad=Used&onesearchad=Nearly%20New&onesearchad=New&advertising-location=at_cars&is-quick-search=TRUE&include-delivery-option=on&page=" + str(max_pages)
driver.get(url)
title = driver.find_elements_by_xpath('//h3[@class="product-card-details__title"]')
price =driver.find_elements_by_xpath('//div[@class="product-card-pricing__price"]')
page_items = len(title)
with open('autotrader.csv', 'a') as f:
for i in range(page_items):
f.write(title[i].text + "," + price[i].text + "\n")
driver.close()
使用 csv.writer
它将正确引用其中包含分隔符的字段:
import csv
# ... code to fetch titles and prices ...
with open('autotrader.csv', 'w', newline='') as f:
w = csv.writer(f)
w.writerow(['Title','Price'])
for t,p in zip(title,price):
w.writerow([t.text,p.text])