我正在尝试从 this website 中提取第一个 ISS TLE(两行元素集) .
我需要前三行:
TWO LINE MEAN ELEMENT SET
文本:(ISS 行,1 行,2 行)。
所以我用漂亮的汤得到了我想要的文本,但后来我真的不知道如何提取这些文本行。我不能使用 split(),因为我需要准确地保留那三行中的空白。如何做到这一点?
import urllib2
from bs4 import BeautifulSoup
import ephem
import datetime
nasaissurl = 'http://spaceflight.nasa.gov/realdata/sightings/SSapplications/Post/JavaSSOP/orbit/ISS/SVPOST.html'
soup = BeautifulSoup(urllib2.urlopen(nasaissurl), 'html.parser')
body = soup.find_all("pre")
index = 0
firstTLE = False
for tag in body:
if "ISS" in tag.text:
print tag.text
请您参考如下方法:
如果您将文本分成几行并一次处理每一行,那么您可以在找到所需的三行时重新加入这些行,例如:
代码:
def process_tag_text(tag_text):
marker = 'TWO LINE MEAN ELEMENT SET'
text = iter(tag_text.split('\n'))
for line in text:
if marker in line:
next(text)
results.append('\n'.join(
(next(text), next(text), next(text))))
return results
测试代码:
import urllib2
from bs4 import BeautifulSoup
nasaissurl = 'http://spaceflight.nasa.gov/realdata/sightings/' \
'SSapplications/Post/JavaSSOP/orbit/ISS/SVPOST.html'
soup = BeautifulSoup(urllib2.urlopen(nasaissurl), 'html.parser')
body = soup.find_all("pre")
results = []
for tag in body:
if "ISS" in tag.text:
results.extend(process_tag_text(tag.text))
print('\n'.join(results))
结果:
ISS
1 25544U 98067A 18054.51611082 .00016717 00000-0 10270-3 0 9009
2 25544 51.6368 225.3935 0003190 125.8429 234.3021 15.54140528 20837
ISS
1 25544U 98067A 18055.54493747 .00016717 00000-0 10270-3 0 9010
2 25544 51.6354 220.2641 0003197 130.5210 229.6221 15.54104949 20991
ISS
1 25544U 98067A 18056.50945749 .00016717 00000-0 10270-3 0 9022
2 25544 51.6372 215.4558 0003149 134.4837 225.6573 15.54146916 21143
ISS
1 25544U 98067A 18057.34537198 .00016717 00000-0 10270-3 0 9031
2 25544 51.6399 211.2932 0002593 130.2258 229.9121 15.54133048 21277
