我练习从网站上抓取数据。这是网站:https : //delicious-fruit.com/ratings/full.php?q=ALL
我的主要目标是收集游戏名称,难度,评分,评分数量,然后用csv保存文件。格式为一字一格,四字一换行。
当我尝试保存文件时,出现了问题。该文件显示一个字符一个单元格,而不是一个单词一个单元格。结果
我认为问题是“for 循环”影响,但不知道解决它。你能给我一些建议吗?我会很感激。
我尝试使用另一个变量来存储数据并将其放入 'writerows' 函数中,但结果保持不变。
from bs4 import BeautifulSoup
import requests
import csv
source = requests.get('https://delicious-fruit.com/ratings/full.php?q=ALL').text
soup= BeautifulSoup(source, 'lxml')
with open ('cms_scrape.csv', 'w', errors='ignore') as csv_file:
writer = csv.writer(csv_file)
table = soup.find('tbody')
table_rows = table.find_all('tr')
for tr in table_rows:
td = tr.find_all('td')
writer.writerows([td[0].text, td[1].text, td[2].text, td[3].text])
csv_file.close()
相关分类