이 코드가 있습니다.
import requests
from xml.dom.minidom import parseString
site = 'test.com'
r = requests.get('http://bar-navig.yandex.ru/u?ver=2&url=http://%s&show=1' % (site))
#print r.text.encode('utf-8')
xmldoc = parseString(r.text.encode('utf-8'))
print xmldoc.getElementsByTagName('tcy')[0].attributes['value'].value
따라서 작동하지만 site
, 예를 들어 'vk.com'
또는 'google.ru'
에 있으면 오류가 발생 xml.parsers.expat.ExpatError: not well-formed (invalid token)
합니다..
그것을 고치는 방법? 감사.
인코딩 문제입니다. XML은 달리 지정하지 않는 한 ASCII 기반이어야합니다. 특히이 XML 소스는 windows-1251
.
이 시도:
parseString(r.text.encode('windows-1251'))
그런 다음 구문 분석 할 수 있습니다.
Minidom은 그다지 영리 unicode
하지 않습니다. 그렇지 않으면 (작동하지 않는) a를 통과했을 때 스스로 알아낼 수 있습니다 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다