Decode a web page using request and BeautifulSoup package

不问归期 提交于 2021-02-10 20:20:55

问题


I am trying a practice question of python. The question is "Use the BeautifulSoup and requests Python packages to print out a list of all the article titles on the New York Times homepage." Below is my solution but it doesn't give any output. I am using Jupyter Notebook and when I run the below code it does nothing. My kernel is also working properly which means I have a problem with my code.

import requests
from bs4 import BeautifulSoup
from urllib.request import urlopen

base_url= 'https://www.nytimes.com/'
r=requests.get(base_url)
soup=BeautifulSoup(urlopen(base_url))

get_titles=soup.find_all(class_="css-1vctqli esl82me2" )

print()
for title in get_titles:
    print(title.text)

回答1:


Where did you get that class tag ? This is not the right one.

You need to replace css-1vctqli esl82me2 by css-1j836f9 esl82me3

import requests
from bs4 import BeautifulSoup
from urllib.request import urlopen

base_url = 'https://www.nytimes.com/'
r = requests.get(base_url)
soup = BeautifulSoup(urlopen(base_url))

get_titles = soup.find_all(class_ = "css-1j836f9 esl82me3")

print()
for title in get_titles:
    print(title.text)

And the output :



来源:https://stackoverflow.com/questions/54117047/decode-a-web-page-using-request-and-beautifulsoup-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!