Python 2.7 : Put extracted data from URL in a CSV file

标签 python python-2.7 csv beautifulsoup

这是我第一次使用Python 2.7,我很喜欢它。但是,我试图弄清楚如何将从 URL 中提取的数据放入 CSV 文件中。我发现this tutorial ,但是当我运行我的脚本时:

# import libraries
import csv
import urllib2
from bs4 import BeautifulSoup

# specify the url
quote_page = 'http://www.bkfrem.dk/default.asp?id=19'

# query the website and return the html to the variable ‘page’
page = urllib2.urlopen(quote_page)

# parse the html using beautiful soup and store in variable soup
soup = BeautifulSoup(page, 'html.parser')

# create CSV file
csvfile = csv.writer(open('firsteam.csv', 'w'))
csvfile.writerow(["Name", "Position"])

# take out the <div> of name and get its value
items = soup.find_all('div', attrs={'class': 'visTruppenContainer'})

for i in range(len(items)):

    playerInfo = items[i].getText(separator=u' ')
    imageURL = items[1].find('img')['src']
    csvfile.writerow([playerInfo, imageURL])
    print (playerInfo)
    print (imageURL)

我收到此错误:

Traceback (most recent call last):
  File "C:/Users/User/Desktop/script2.py", line 26, in <module>
    csvfile.writerow([playerInfo, imageURL])
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe5' in position 27: ordinal not in range(128)

我做错了什么?在写入 CSV 文件之前是否必须转换数据?

最佳答案

您需要对 playerInfo 进行编码,如下所示:

代码:

csvfile.writerow([playerInfo.encode('utf-8'), imageURL])

结果:

1. Marco Brylov Position: Målmand Højde: 191 Vægt: 92 Født: 21-11-1995
http://www.bkfrem.dk/images/spillere/02_mikkel_andersson.jpg
2. Mikkel Andersson Position: Midtbane Højde: 170 Vægt: 67 Født: 17-03-1990
http://www.bkfrem.dk/images/spillere/02_mikkel_andersson.jpg
3. Casper Andersen Position: Midtstopper Højde: 190 Vægt: 90 Født: 04-08-1982
http://www.bkfrem.dk/images/spillere/02_mikkel_andersson.jpg
...

关于Python 2.7 : Put extracted data from URL in a CSV file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48727150/

相关文章:

python - matplotlib 标签的关键字参数的多个值

python - 为什么 matplotlib 需要在 plt.scatter() 之前设置日志比例而不是 plt.plot()?

angularjs - 使用 Angularjs 将 Excel 转换为 JSON

python - Github Actions Build - 在多个文件夹上推送到 ECR

python - 从 Django Haystack 获取原始结果

python - 使用 "Next"按钮 Python 进行 Web 抓取

php - 通过 PHP 从 MySQL 导出到 CSV

python - 使用 Python 读取 CSV 文件

python - Pandas csv itertools 组合

python - peewee:过滤从多对多关系中选择查询结果