Python Forum
[SOLVED] [BeautifulSoup] How to get this text?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] [BeautifulSoup] How to get this text?
#1
Hello,

Can BeautifulSoup grab what's between the brackets ("John Doe") in the following line?

from bs4 import BeautifulSoup

with open("input.txt") as fp:
    soup = BeautifulSoup(fp, 'html.parser')

#<a href="/authorx/john_doe">John Doe</a>
Thank you.

--
Edit: Found it

items = soup.select("a[href*=authorx]")
for item in items:
	#print(item)
	print(item.string)
Reply
#2
There is no need to loop to get the text.
from bs4 import BeautifulSoup

html = '<a href="/authorx/john_doe">John Doe</a>'
soup = BeautifulSoup(html, 'html.parser')
>>> item = soup.find('a')
>>> item.text
'John Doe'
Or if use CSS selector then can use select_one() if only need this element.
>>> item = soup.select_one("a[href*=authorx]")
>>> item.text
'John Doe' 
Reply
#3
Even if a book has more than one author, and the page has a bunch of href links that have nothing to do with the authors?
Reply
#4
(Aug-17-2022, 02:19 PM)Winfried Wrote: Even if a book has more than one author, and the page has a bunch of href links that have nothing to do with the authors?
Give a example if you have trouble.
There are serval to get a tag even if there are serval similar.
from bs4 import BeautifulSoup

html = '''\
<body>
  <a href="/authorx/john_doe">John Doe1</a>
  <a href="/authorx/john_doe">John Doe2</a>
  <a href="/authorx/john_doe">John Doe3</a>
</body>'''

soup = BeautifulSoup(html, 'html.parser') 
>>> item = soup.select_one('body > a:nth-child(2)')
>>> item.text
'John Doe2'
Reply
#5
The code above works fine, so I'm happy with it.

However, what about this?

<div class="bi_row">
<span class="bi_col_title">Publication date</span>
<span class="bi_col_value">January 1, 1999</span>
</div>


How could I get the publication date ("January 1, 1999") ?
Reply
#6
from bs4 import BeautifulSoup

html = '''\
<div class="bi_row">
  <span class="bi_col_title">Publication date</span>
  <span class="bi_col_value">January 1, 1999</span>
</div>'''

soup = BeautifulSoup(html, 'html.parser')
# CSS selector
>>> item = soup.select_one("span.bi_col_value")
>>> item.text
'January 1, 1999'

# Using find() add a singel _ in CSS class
>>> item = soup.find(class_="bi_col_value")
>>> item.text
'January 1, 1999'

# Get attribute would be like
>>> item.attrs
{'class': ['bi_col_value']}
>>> item.get('class')
['bi_col_value']
Reply
#7
Sorry, forgot to say the webpage contains multiple items with identical elements:

<div class="bi_row">
<span class="bi_col_title">Publisher</span>
<span class="bi_col_value">Some publisher Inc</span>
</div>

<div class="bi_row">
<span class="bi_col_title">Publication date</span>
<span class="bi_col_value">January 1, 1999</span>
</div>


etc.

--
Edit: Kludgy but it works:

for col in soup.find_all("div", {"class": "bi_row"}):
	if col.find("span", {"class": "bi_col_title"}).text == "Publication date":
		print(col.find("span", {"class": "bi_col_value"}).text)
		break
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [BeautifulSoup] Why attribute not found? Winfried 0 751 Mar-11-2023, 10:00 PM
Last Post: Winfried
  [SOLVED] [BeautifulSoup] Why does it turn inserted string's brackets into &lt;/&gt;? Winfried 0 1,523 Sep-03-2022, 11:21 PM
Last Post: Winfried
  [SOLVED] [Beautifulsoup] Find if element exists, and edit/append? Winfried 2 4,306 Sep-03-2022, 10:14 PM
Last Post: Winfried
  [SOLVED] [BeautifulSoup] Turn select() into comma-separated string? Winfried 0 1,120 Aug-19-2022, 08:07 PM
Last Post: Winfried
  Delete empty text files [SOLVED] AlphaInc 5 1,568 Jul-09-2022, 02:15 PM
Last Post: DeaD_EyE
  [SOLVED] [ElementTree] Grab text in attributes? Winfried 3 1,638 May-27-2022, 04:59 PM
Last Post: Winfried
  [SOLVED] Read text file from some point till EOF? Winfried 1 1,959 Oct-10-2021, 10:29 PM
Last Post: Winfried
  Sorting and Merging text-files [SOLVED] AlphaInc 10 4,891 Aug-20-2021, 05:42 PM
Last Post: snippsat
Thumbs Up [SOLVED] Find last occurence of pattern in text file? Winfried 4 4,407 Aug-13-2021, 08:21 PM
Last Post: Winfried
  Replace String in multiple text-files [SOLVED] AlphaInc 5 8,140 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt

Forum Jump:

User Panel Messages

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