CCheung
CCheung

Reputation: 3

I am using Beautiful Soup to obtain headline from website but it returns nothing

I am using Beautiful Soup to obtain headline from website but it returns nothing...

May I know how to improve the code?


from bs4 import BeautifulSoup
import requests 

source = requests.get('https://news.yahoo.co.jp/').text

soup = BeautifulSoup(source, 'lxml')

target = soup.find('div',id ='accr')

for target in soup.find_all('target'):

    heading = target.find('p',class_='yjnSub_list_headline')
    print(heading.text)

    source = target.find('span',class_='yjnSub_list_sub_media')
    print(source.text)

    date = target.find('time',class_='yjnSub_list_sub_date')
    print(date.text)

Upvotes: 0

Views: 36

Answers (1)

aparpara
aparpara

Reputation: 2201

The line target = soup.find('div',id ='accr') has no effect, because you don't use the result. And soup.find_all('target') doesn't make sense, because there is no element <target> on the page. Instead you need soup.find_all('div', class_='yjnSub_list_text'), because that is the parent element for the elements you want to print.

Upvotes: 1

Related Questions