2014-05-10 55 views
-2

我試圖讓英超表使用此代碼獲取:如何轉換unicode的,我要和BeautifulSoup

from bs4 import BeautifulSoup 
import requests 
url ="http://www.premierleague.com/en-gb.html" 
r = requests.get(url) 
soup = BeautifulSoup(r.content) 
table = soup.find('table', {'class': 'leagueTable'}) 
rows = table.findAll('tr') 
data = [[td.text for td in tr.findAll("td")] for tr in rows] 

for i in data: 
    print i 

一切都運行完美,但我得到的Unicode中的結果。如何將其轉換爲純文本?

+1

這是有幫助嗎? http://stackoverflow.com/questions/1207457/convert-unicode-to-a-string-in-python-containing-extra-symbols – StefanNch

+0

你可以編碼你的Unicode蜇傷。但爲什麼你想要它? – cubuspl42

回答

1

您可以將行更改爲text.encode("utf-8")

data = [[td.text.encode("utf-8") for td in tr.findAll("td")] for tr in rows] 
or str(td.text) 

大量的信息在BeautifulSoup docs