python - 将抓取的数据写入 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()
预期输出:
列分隔输出
我得到的输出: 在单行中获取输出
任何帮助表示赞赏
解决方案
推荐阅读
- javascript - 当我重写滚动条css属性时,textarea样式的resize图标发生了变化
- authentication - 在对 Azure 静态网站进行身份验证时检索组
- delphi - 如何禁用 TWebBrowser 上下文菜单?
- robotframework - 如何将参数传递给机器人库
- java - LinkedBlockingQueue 何时优于 ConcurrentLinkedQueue?
- arrays - 对象数组内的弹性搜索查询
- python - 带有查询返回值的 Sqlite OperationalError
- reactjs - 在 render() 中反应 setState
- android - 无法获取“https://google.bintray.com/flexbox-layout/com/google/android/flexbox/1.0.0/flexbox-1.0.0.pom”
- python - 使用 Python Cx_Oracle 将 7L 行的 185 列插入到 Oracle sql 表中