python - 使用 lxml/requests 从网页进行 TLE (Python3.6.4)

标签 python python-3.x python-requests lxml

我正在尝试从此 link. 读取 TLE 集 链接来自Space-Track.org

我想读取链接并创建两个变量,一个变量对应两行元素的每一行。我做了一些搜索,并尝试重用此线程 about reading a particular line from a web page in Python 中的代码。但是,我对 requests 或 lxml 都不熟悉。我尝试运行下面的代码:

<import numpy as np
from lxml import html
import requests

for nn in range(0,2):
    page = requests.get("https://www.space-track.org/basicspacedata/query/class/tle_latest/ORDINAL/1/NORAD_CAT_ID/39090/orderby/TLE_LINE1%20ASC/format/tleid=%" % nn)
    tree = html.fromstring(page.text)
    print (tree.xpath("//b/text()")[0])
>

并得到以下回溯错误:

<Traceback (most recent call last):
  File "C:\Python36-64\Projects\HTMLTLEREADER.py", line 19, in <module>
    page = requests.get("https://www.space-track.org/basicspacedata/query/class/tle_latest/ORDINAL/1/NORAD_CAT_ID/39090/orderby/TLE_LINE1%20ASC/format/tleid=%" % nn)
ValueError: unsupported format character 'A' (0x41) at index 115
>

我最初尝试读取这两行来检查代码是否在我的 python 版本上运行,因此 nn 的 range(0,2) 。

感谢任何和所有帮助。

谢谢。

最佳答案

错误消息非常清楚:

ValueError: unsupported format character 'A' (0x41) at index 115

您的字符串的索引 115 位于此处:

"...NORAD_CAT_ID/39090/orderby/TLE_LINE1%20ASC/format/tleid=%" % nn
                                           ↑

您正在使用百分比格式,但您的字符串已包含 URL 中不相关的百分比编码的百分号。您可以对现有百分号进行双重转义并修复最后一个格式字符串:

"...NORAD_CAT_ID/39090/orderby/TLE_LINE1%%20ASC/format/tleid=%s" % nn
                                         ↑                    ↑

或者只是连接字符串:

"...NORAD_CAT_ID/39090/orderby/TLE_LINE1%20ASC/format/tleid=" + str(nn)

关于python - 使用 lxml/requests 从网页进行 TLE (Python3.6.4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48587811/

相关文章:

Python 请求和 Meteor/MongoDB collectionapi 更新不起作用

python-3.x - 多线程爬虫运行一段时间后越来越慢

python - 如何将 HTML 代码集成到 Python 脚本中?

python - 使用 Python 进行高效重采样

python - 检查文本文件python中是否存在单词

python-3.x - 从 Azure Blob 存储、Flask Python、HTML/JS 检索并显示?

python - Pyodbc 从今天开始使用我的 MSSQL

python-3.x - 在 GitLab 中创建一个 pyqt 构建

python - Python 3 中的 urllib 使用

python - 请求.exceptions.SSLError : hostname 'boxfwd.com' doesn't match either of 'nycmsk.com' , 'www.nycmsk.com'