简体   繁体   English

Python 2.7:将从URL中提取的数据放入CSV文件中

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

This is my first time using Python 2.7, I do like it. 这是我第一次使用Python 2.7,我很喜欢它。 However, I am trying to figure out how I can put extracted data from the URL into a CSV file. 但是,我试图弄清楚如何将URL中提取的数据放入CSV文件中。 I found this tutorial , but when I run my script: 我找到了本教程 ,但是在运行脚本时:

# 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)

I get this error: 我收到此错误:

 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) 

What am I doing wrong? 我究竟做错了什么? Do I have to convert the data before writing to the CSV file? 在写入CSV文件之前是否必须转换数据?

You will need to encode the playerInfo like: 您将需要对playerInfo进行编码,例如:

Code: 码:

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

Results: 结果:

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
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM