Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lxml-html read produces empty list Python 3.6.4

I am trying to read the two line elements for STRaND-1 from this link: https://celestrak.org/NORAD/elements/cubesat.txt, so I can track it from a ground station I'm building. I don't really understand how to use the xtree.xpath command an I'd like to learn how. I'm trying the following code that I found from a similar question asked on here a while ago:

import numpy as np
from lxml import html
import requests
line_number = 50
for word in range(0,5):
    page = requests.get("https://celestrak.org/NORAD/elements/cubesat.txtid=%s" % word)
    tree = html.fromstring(page.text)
    print (tree.xpath("//b/text()")

This should print the code between the elements of the html page right? How do I just print from a certain line? Expecially when there is no html prefix before the text that I want?

Thanks for your time.

like image 457
Lorcank11 Avatar asked May 31 '26 16:05

Lorcank11


1 Answers

Try below solution to get required data:

import requests

url = "http://celestrak.com/NORAD/elements/cubesat.txt"
response = requests.get(url)

page_content = response.text
all_lines = [line.strip() for line in page_content.split("\n")]
for index, line in enumerate(all_lines):
    if line == "STRAND-1":
        first_value = all_lines[index + 1]
        second_value = all_lines[index + 2]
        break

print(first_value, "\n", second_value)

Output:

1 39090U 13009E   18037.58367953  .00000016  00000-0  21168-4 0  9998 
 2 39090  98.5328 245.5663 0008674 331.4360  28.6349 14.35009671259097
like image 101
Andersson Avatar answered Jun 03 '26 07:06

Andersson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!