Python Forum
Beautiful Soup - Title + Paragraph into a text file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beautiful Soup - Title + Paragraph into a text file
#1
Hi all,



I am trying to extract a heading and a title, there is something not quite right about this



from bs4 import BeautifulSoup
 
html = '''\
<h2 class="Title">section1</h2>
<p class ="mainparagraph">article1</p>
<p>article2</p>
<p>article3</p>
<h2>section2</h2>
<span class="1"> hello 1 </span>
<p>article4</p>
<p>article5</p>
<h2 class="2"> hello </h2>
<p>article6</p>
<span class="2"> hello 2 </span>
<h1> Lorem Ipsum</h1>
<p> 1 Lorem ipsum dolor </p>
<h2> Lorem Ipsum</h1>
<p> 2 Lorem ipsum dolor </p>
<h1> Lorem Ipsum</h1>
<p> 3 Lorem ipsum dolor </p>",'lxml')
'''

soup = BeautifulSoup(html, 'lxml') 

#soup = BeautifulSoup(open("a.html"),'lxml')


links = soup.findAll('h2', {'class': ['Title']},limit=1)       


with open('New.txt','w') as Output_File:
    for link in links:
        names1 = link.contents[0]
        links = soup.find('p', {'class': ['mainparagraph']})
        names2 = link.contents[0]
        names2.extract()

        
        Output_File.write(print,names1.extract()+ '\n', names2.extract())


I am not sure if I am meant to append the results ?



thank you



:)


Python newbie trying to learn the ropes
Reply


Messages In This Thread
Beautiful Soup - Title + Paragraph into a text file - by dj99 - Jul-14-2018, 10:52 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  <title> django page title dynamic and other field (not working) lemonred 1 2,115 Nov-04-2021, 08:50 PM
Last Post: lemonred
  Beautiful Soup - access a rating value in a class KatMac 1 3,479 Apr-16-2021, 01:27 PM
Last Post: snippsat
  *Beginner* web scraping/Beautiful Soup help 7ken8 2 2,627 Jan-28-2021, 04:26 PM
Last Post: 7ken8
  Help: Beautiful Soup - Parsing HTML table ironfelix717 2 2,703 Oct-01-2020, 02:19 PM
Last Post: snippsat
  Beautiful Soup (suddenly) doesn't get full webpage html j.crater 8 16,994 Jul-11-2020, 04:31 PM
Last Post: j.crater
  Requests-HTML vs Beautiful Soup - How to Choose? robin73 0 3,833 Jun-23-2020, 02:53 PM
Last Post: robin73
  looking for direction - scrappy, crawler, beautiful soup Sly_Corn 2 2,470 Mar-17-2020, 03:17 PM
Last Post: Sly_Corn
  Beautiful soup truncates results jonesjoz 4 3,897 Mar-09-2020, 06:04 PM
Last Post: jonesjoz
  Getting a specific text inside an html with soup mathieugrimbert 9 15,964 Jul-10-2019, 12:40 PM
Last Post: mathieugrimbert
  Beautiful soup and tags starter_student 11 6,201 Jul-08-2019, 03:41 PM
Last Post: starter_student

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020