google.com의 xml을 구문 분석하려고하지만 '잘 형성되지 않은'오류가 발생합니다. 왜 이런거야? 감사
➜ testing cat code.py
from urllib.request import urlopen; from xml.etree.ElementTree import fromstring
fromstring(urlopen('https://www.google.com').read().replace(b'<!doctype html>',b'<!DOCTYPE html>'))
➜ testing python3 code.py
Traceback (most recent call last):
File "code.py", line 2, in <module>
fromstring(urlopen('https://www.google.com').read().replace(b'<!doctype html>',b'<!DOCTYPE html>'))
File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/xml/etree/ElementTree.py", line 1315, in XML
parser.feed(text)
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 1826
➜ testing
XML 구문 분석기로 HTML을 구문 분석하려고하기 때문에 오류 메시지가 표시 될 수 있습니다. 작동하지 않습니다. HTML 파서가있는 라이브러리를 사용해보십시오. 또한 대신 요청이있는 페이지를 가져 오는 것이 좋습니다. 그래서 함께 :
import requests
import lxml.html as lh
req = requests.get('https://www.google.com')
lh.fromstring(req.text)
작동합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다