Lorcank11
Lorcank11

Reputation: 147

TLE from web page using lxml/requests (Python3.6.4)

I'm trying to read a TLE set from this link. The link comes from Space-Track.org

I want read the link and create two variables, one for each line of the two line element in doing so. I did some searching and am trying to reuse the code from this thread about reading a particular line from a web page in Python. However, I'm not familiar with either requests or lxml. I tried running the code below:

<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])
>

And got the following traceback error:

<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
>

I was initially trying to read both lines to check if the code ran on my version of python, hence range(0,2) for nn.

Any and all help appreciated.

Thanks.

Upvotes: 1

Views: 155

Answers (1)

Blender
Blender

Reputation: 298392

The error message is pretty clear:

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

Index 115 of your string is here:

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

You're using percent-formatting but your string already contains percent signs for the unrelated percent-encoding in the URL. You can either double-escape your existing percent signs and fix your last format string:

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

Or just concatenate the string:

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

Upvotes: 2

Related Questions