返回空值
!pip install bs4
from bs4 import BeautifulSoup
import requests
def get_news_of(keyword):
content = requests.get('https://www.nytimes.com/')
soup = BeautifulSoup(content.content, 'html.parser')
#Add your code below.
#use soup.find_all('a') like below
all_a = soup.find_all('a')
return_list = []
#iterate over all the items in all_a and get by 'class' name
#then check if keyword is present
for tag_a in all_a:
a_title = tag_a.get_text('head',[]).strip()
link = tag_a.get('href',[]).strip()
for elem in a_title:
if keyword in elem:
return_list=a_title.append(link)
return return_list
没有运行结果
尝试append 及字典无结果
返回有关的文章标题名及链接
看你打开的连接都是外网地址吧,我这里都打不开的,你应该改成国内https://cn.ibnews.com/nytimes这个地址吧