Вам не нужно использовать красивый суп для очистки этого URL.
https://mars.nasa.gov/news/?page=0&per_page=40&order=publish_date+desc%2Ccreated_at+desc&search=&category=19%2C165%2C184%2C204&blank_scope=Latest
Когда я проверял вкладку сети, я обнаружил, что эта страница на самом деле получает тело статьи, используя JSON, полученный из запроса API, которыйможет быть легко сделано с помощью библиотеки requests
.
Вы можете попробовать следующий код
import requests
import json # need to use this to pretty print json easily
headers = {
'user-agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:49.0) Gecko/20100101 Firefox/49.0' # to mimic regular browser user-agent
}
params = (
('page', '0'),
('per_page', '40'), # you tweak this value to 1000 or maybe more to get more data from a single request
('order', 'publish_date desc,created_at desc'),
('search', ''),
('category', '19,165,184,204'),
('blank_scope', 'Latest'),
)
# params is equivalent to page=1&per_page=40&order=publish_date+desc%2Ccreated_at+desc&search=&category=19%2C165%2C184%2C204&blank_scope=Latest
r = requests.get('https://mars.nasa.gov/api/v1/news_items/', params=params, headers=headers).json()
print(json.dumps(r, indent=4)) # prints the raw json respone
'''
The article data is contained inside the key `"items"`, we can iterate over `"items"` and
print article title and body. Do check the raw json response to find
other data included along with article title and body. You just need
to use the key name to get those values like you see in the below code.
'''
for article in r["items"]:
print("Title :", article["title"])
print("Body :",article["body"])
Смотрите это в действии здесь .