그래서 웹에서 oed.com에이 웹 페이지를 긁을 때마다 유니 코드 문자로 보이는 작은 아포스트로피가 나타납니다. 내 코드를 필터링하고 모든 문자를 일반 아포스트로피로 바꿀 수 있습니까? 다음은 내가 사용한 단어 목록을 인쇄 할 때 사용한 코드입니다 (사이트에 로그인하지 않은 경우 여러 번 긁으면 반복되는 단어가 표시됩니다).파이썬에서 웹 스크랩을 할 때 유니 코드 문자를 걸러내는 방법은 무엇입니까?
import csv
import os
import re
import requests
import urllib2
year_start= 1550
year_end = 1560
subject_search = ['Law']
with open("/Applications/Python 3.5/Economic/OED_table.csv", 'a') as outputw, open("/Applications/Python 3.5/Economic/OED.html", 'a') as outputh: #opens the folder and 'a' adds the words to the csv file.
for year in range(year_start, year_end +1):
path = '/Applications/Python 3.5/Economic'
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
urllib2.install_opener(opener)
user_agent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)'
header = {'User-Agent':user_agent}
resultPath = os.path.join(path, 'OED_table.csv')
htmlPath = os.path.join(path, 'OED.html')
request = urllib2.Request('http://www.oed.com/search?browseType=sortAlpha&case-insensitive=true&dateFilter='+ str(year)+ '&nearDistance=1&ordered=false&page=1&pageSize=100&scope=ENTRY&sort=entry&subjectClass='+ str(subject_search)+ '&type=dictionarysearch', None, header)
page = opener.open(request)
urlpage = page.read()
outputh.write(urlpage)
new_words = re.findall(r'<span class=\"hwSect\"><span class=\"hw\">(.*?)</span>', urlpage)
print new_words
csv_writer = csv.writer(outputw)
if csv_writer.writerow([year] + new_words):
csv_writer.writerow([year, word])
이 단어가 인쇄 된 후에는 종종 유니 코드 문자 \ xcb \ x88이 표시됩니다. 예를 들어 un'sentenced 단어는 'un \ xcb \ x88sentenced'로 인쇄됩니다.
유니 코드 문자의 모든 인스턴스를 가져 와서 해당 아포스트로피로 바꿀 수있는 방법> '. 나는 그것이 이런 식으로 될 것이라고 생각하고 있었다.
for word in new_words:
word = re.sub('[\x00-\x7f]','', word)
나는 걸렸다.
이 문자를 제거하거나 유니 코드로 올바르게 해석 하시겠습니까? https://docs.python.org/2/howto/unicode.html을 참조하십시오. 가능한 경우, 유니 코드를 다루는 것이 훨씬 더 나은 Python 3로 전환하는 것이 좋습니다. – amyrit
@amyrit, 기본적으로 문자를 제거하고 간단한 키보드 아포스트로피 문자로 바꾸기를 원합니다.> ' – Kainesplain
'word.replace ('xcb \ x88 ', "'")'를 사용해 보셨습니까? 이 문제의 일부만 해결할 수 있지만, 유니 코드를 제대로 처리하는 것이 좋습니다. 이것이 실생활 코딩이라면, 그것을 피할 수는 없습니다. – amyrit