python - 尝试将站点分别抓取到 xml 和 json 时未找到元素/期望值
问题描述
我一直在尝试解析网站一段时间,但在尝试解析网站时不断出错。当我尝试使用 XML 时,我得到了这个:
xml.etree.ElementTree.ParseError:找不到元素:第 1 行,第 1 列
当我尝试使用 json 时,我得到了这个:
json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0)
我的 xml 代码是:
import urllib.request, urllib.parse, urllib.error
import ssl
import xml.etree.ElementTree as PT
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
while True:
url= input('Enter Location: ')
print('Retrieving', url)
uh = urllib.request.urlopen(url, context=ctx)
data = uh.read()
print('Retrieved', len(data), 'characters')
tree=PT.fromstring (uh.read())
print (tree)
break
我的 json 代码是:
import urllib.request, urllib.parse, urllib.error
import ssl
import json
import requests
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
while True:
url= input('Enter Location: ')
print('Retrieving', url)
r = requests.get(url, auth=('user', 'pass'))
m=r.json()
print (m)
break
解决方案
推荐阅读
- c# - 我使用 html.beginform 从控制器的视图发送信息,它改变了页面的整个视图 - ASP.NET
- android - Android 媒体播放器 - 无法设置数据源 - URL
- html - 为什么我的网格单元没有占用它们分配的 fr 单元?
- java - Android Studio 问题:W/RecyclerView:未连接适配器;跳过布局
- c - 使用链表下降优先级队列而不以优先级作为输入
- python - 在 tkinter 中引入新小部件时将小部件保留在其位置
- jakarta-ee - 如何使用 Quarkus 在生产环境中禁用从 /resources/META-INF/resources 访问某些静态文件?
- python - Python多处理运行过程两次?
- javascript - 如何让 JSON 数据返回?
- c++ - 在 gcc 路径搜索中 - 我无法在当前目录中导入文件