使用请求和BeautifulSoup软件包解码网页 [英] Decode a web page using request and BeautifulSoup package

查看:50
本文介绍了使用请求和BeautifulSoup软件包解码网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试python的练习问题.问题是使用BeautifulSoup并请求Python程序包在《纽约时报》首页上打印所有文章标题的列表."以下是我的解决方案,但未提供任何输出.我正在使用Jupyter Notebook,当我运行以下代码时,它什么也没做.我的内核也正常工作,这意味着我的代码有问题.

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)

推荐答案

您从何处获得该类标记?这不是正确的.

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

您需要将 css-1vctqli esl82me2 替换为 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)

输出:

这篇关于使用请求和BeautifulSoup软件包解码网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆