Home > Back-end >  beautifulsoup with same name tag, but different use
beautifulsoup with same name tag, but different use

Time:07-14

samediv

So i have this code

for j in links:
    Title = j.find('p').text.strip()
    Narration = j.find('p').text.strip()

    Link = j.find('a')['href'].strip()
    Date = j.find('time').text[-14:-1].strip()
    Label = '1'

But i have a trouble with it because theres 2 same <p> tag within the same div,but it has different use

Title = News Title
Narration = News Narration

is there any way to differentiate the <p> tags?

CodePudding user response:

It needs some HTML code to clarify, but as I understand correct you have to use find_next('p') on the first find('p'):

for j in links:
    Title = j.find('p').text.strip()
    Narration = j.find('p').find_next('p').text.strip()
  • Related