Siemka ćwiczę ostatnio na stronce https://www.practicepython.org/exercise/2014/06/06/17-decode-a-web-page.html
Treść zadania:
Use the BeautifulSoup and requests Python packages to print out a list of all the article titles on the New York Times homepage.
Moje rozwiązanie:
import requests
from bs4 import BeautifulSoup
url = 'https://www.nytimes.com/'
r = requests.get(url)
r_html = r.text
features = "html.parser"
soup = BeautifulSoup(r_html, features)
titles = soup.find_all('title')
for each_title in titles:
print(each_title.string)
Wynik:
The New York Times - Breaking News, World News & Multimedia
Share Article: "Leslie Moonves, Longtime CBS Chief, Steps Down"
Share Article: "A Spy Story: Sergei Skripal Was a Small Fish With a Big Enemy"
Share Article: "The Unglamorous Work of Looking for an Assassin"
Share Article: "U.S. to Issue New Sanctions Against Russia Over Spy Poisoning"
Share Article: "Trump Claims Credit for the Economy. Not So Fast, Says Obama."
Share Article: "Trump Says Ford Can Make New Car at Home. Ford Says It Won’t."
Share Article: "Is This a Mid-1990s Moment for the Economy? Three Reasons for Optimism"
Share Article: "California Tries New Tack on Gun Violence: Ammo Controls"
Share Article: "Teaching in the Age of School Shootings"
Share Article: "My Father, a Judge, Said a Gun Control Case Was One of His Hardest. Now I See Why."
Share Article: "C.I.A. Drone Mission, Curtailed by Obama, Is Expanded in Africa"
Share Article: "How a U.S. Open Final Descended Into Chaos"
Share Article: "Djokovic Wins U.S. Open for His 14th Grand Slam Title"
Share Article: "Williams Picks the Wrong Time to Make the Right Point"
Share Article: "Don’t Let Migrant Kids Rot"
Share Article: "Can You Win an Election by Talking About Segregation?"
Share Article: "In 1993, ‘Joy Luck Club’ Changed Hollywood. Until It Didn’t."
Share Article: "The Chicest Store in Milan Comes to New York City"
Share Article: "An Epic From Iceland, Complete With Unicorns and Angels"
Jednak gdy wchodzę w oficjalne rozwiązanie (https://www.practicepython.org/solution/2014/07/10/17-decode-a-web-page-solutions.html) to dlaczego autor i większość ludzi szuka
class_="story-heading"
zamiast zwyczajnie contentu zawartego w <title></title>?
Czemu jak wchodzę w Źródło strony NYTimes to w HTML nie mogę znaleźć nic na temat class_="story-heading" ?
To zadanie z 2014 roku i nie jestem pewien czy zrobiłem je dobrze, z góry dzięki za pomoc