python csvwriter 在不同的行上输出字符串,而不是在同一行上

python csvwriter outputs strings on seperate rows instead of being on the same row

一段时间后,我重新开始为一个研究项目编写代码,我目前正在做一个练习网站,看看我需要为实际网站做些什么。

我的一切都按照我想要的方式工作,但是当抓取的数据输出到 csv 时,它会将值放入一个新行而不是该行旁边的列。

我在下面的输出中添加了链接。让我知道我需要更改什么,因为我无法弄明白。

import csv, re
import requests 
from bs4 import BeautifulSoup

URL = "https://realpython.github.io/fake-jobs/"
page = requests.get(URL)
with open('testScraperEX.csv', 'w') as f:
    write = csv.writer(f)
    soup = BeautifulSoup(page.content, "html.parser")
    write.writerow(['Title', 'Company', 'Location'])
    results = soup.find(id="ResultsContainer")
    job_elements = results.find_all("div", class_="card-content")
    for job_element in job_elements:
        title_element = job_element.find("h2", class_="title")
        company_element = job_element.find("h3", class_="company")
        location_element = job_element.find("p", class_="location")
        Title = title_element.text.strip()
        Company = company_element.text.strip()
        Location = location_element.text.strip()
        write.writerows([[Title],[Company],[Location]])

这是当前输出

这就是我想要的输出方式

谢谢:)

import csv
import requests 
from bs4 import BeautifulSoup

URL = "https://realpython.github.io/fake-jobs/"
page = requests.get(URL)
with open('testScraperEX.csv', 'w', newline='') as f:
    write = csv.writer(f)
    soup = BeautifulSoup(page.content, "html.parser")
    write.writerow(['Title', 'Company', 'Location'])
    results = soup.find(id="ResultsContainer")
    job_elements = results.find_all("div", class_="card-content")
    for job_element in job_elements:
        title_element = job_element.find("h2", class_="title")
        company_element = job_element.find("h3", class_="company")
        location_element = job_element.find("p", class_="location")
        Title = title_element.text.strip()
        Company = company_element.text.strip()
        Location = location_element.text.strip()
        write.writerow([Title, Company, Location])

在csv文件中输出:

Title,Company,Location
Senior Python Developer,"Payne, Roberts and Davis","Stewartbury, AA"
Energy engineer,Vasquez-Davidson,"Christopherville, AA"
Legal executive,"Jackson, Chambers and Levy","Port Ericaburgh, AA"
Fitness centre manager,Savage-Bradley,"East Seanview, AP"
Product manager,Ramirez Inc,"North Jamieview, AP"
... and many more lines ...

你代码中的这一行

write.writerows([[Title],[Company],[Location]])

写了 3 行,因为每个元素都不必要地在一个列表中(即你有 3 个 one-element 列表的列表)。这个

write.writerow([Title, Company, Location]) 

写入一行 - 包含 3 个元素的列表。

注意 wrte.writerowswrite.writerow[[Title],[Company],[Location]][Title, Company, Location]