Python UnicodeEncodeError:&x27;ascii';编解码器可以';t编码字符u'\u2730';位置1:序号不在范围内(128)
你知道怎么解决这个问题吗Python UnicodeEncodeError:&x27;ascii';编解码器可以';t编码字符u'\u2730';位置1:序号不在范围内(128),python,unicode,Python,Unicode,你知道怎么解决这个问题吗 import csv import re import time import urllib2 from urlparse import urljoin from bs4 import BeautifulSoup BASE_URL = 'http://omaha.craigslist.org/sys/' URL = 'http://omaha.craigslist.org/sya/' FILENAME = '/Users/mona/python/craigstvs.t
import csv
import re
import time
import urllib2
from urlparse import urljoin
from bs4 import BeautifulSoup
BASE_URL = 'http://omaha.craigslist.org/sys/'
URL = 'http://omaha.craigslist.org/sya/'
FILENAME = '/Users/mona/python/craigstvs.txt'
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
soup = BeautifulSoup(opener.open(URL))
with open(FILENAME, 'a') as f:
writer = csv.writer(f, delimiter=';')
for link in soup.find_all('a', class_=re.compile("hdrlnk")):
timeset = time.strftime("%m-%d %H:%M")
item_url = urljoin(BASE_URL, link['href'])
item_soup = BeautifulSoup(opener.open(item_url))
# do smth with the item_soup? or why did you need to follow this link?
writer.writerow([timeset, link.text, item_url])
作为一个经验,我不得不说csv模块并不完全支持unicode,但您可能会发现以这种方式打开文件很有用
import codecs
...
codecs.open('file.csv', 'r', 'UTF-8')
或者您可能希望自己处理,而不是使用csv模块您只需要对文本进行编码:
link.text.encode("utf-8")
您还可以使用请求
代替urllib2:
import requests
BASE_URL = 'http://omaha.craigslist.org/sys/'
URL = 'http://omaha.craigslist.org/sya/'
FILENAME = 'craigstvs.txt'
soup = BeautifulSoup(requests.get(URL).content)
with open(FILENAME, 'a') as f:
writer = csv.writer(f, delimiter=';')
for link in soup.find_all('a', class_=re.compile("hdrlnk")):
timeset = time.strftime("%m-%d %H:%M")
item_url = urljoin(BASE_URL, link['href'])
item_soup = BeautifulSoup(requests.get(item_url).content)
# do smth with the item_soup? or why did you need to follow this link?
writer.writerow([timeset, link.text.encode("utf-8"), item_url])