首页 > 解决方案 > 将抓取的数据写入 CSV 文件中的新列

问题描述

大家好,我正在使用 csv 模块写入我已抓取的数据,但每个打印语句数据必须存储在下一列中,但正在将其写入同一列

我正在尝试这段代码,但没有得到想要的输出

from bs4 import BeautifulSoup
from requests_html import HTMLSession
from urllib.request import Request, urlopen
import re
import CSV

s = HTMLSession()
list_data = []
url = f'https://everymac.com/systems/apple/iphone/index-iphone-specs.html'
r = s.get(url)
r.html.render(timeout=16, sleep=1)
soup = BeautifulSoup(r.html.html, 'html.parser')

file = open('output.csv', 'w')
writer = csv.writer(file)

products = soup.select('#contentcenter_specs_externalnav_2 a')
for item in products:
    title_text = item.get_text(strip=True)
    print(title_text)

    writer.writerow([title_text])

rows = soup.select('tr:nth-child(4) td , tr:nth-child(3) td , tr:nth-child(2) td , tr:nth-child(1) td')
for row in rows:
    clean_text = row.get_text(strip=True)
    a = clean_text
    print(a)

    writer.writerow([a])

links = soup.select('#contentcenter_specs_externalnav_2 a')
for link in links:
    x = link.get('href')
    lurl = 'https://everymac.com' + str(x)
    # print(lurl)

    req = Request(lurl, headers={'User-Agent': 'Mozilla/5.0'})
    webpage = urlopen(req).read()
    sp = BeautifulSoup(webpage, 'html.parser')
    for tr in sp.find_all('tr'):
        for td in tr.select('#content28-title td:nth-child(2)', string=re.compile("^US[$]")):
            y = td.text
            print(y)

            writer.writerow([y.encode('utf-8')])
file.close()

预期输出:

列分隔输出

在此处输入图像描述

我得到的输出: 在单行中获取输出

在此处输入图像描述

任何帮助表示赞赏

标签: pythoncsvweb-scraping

解决方案


推荐阅读