7 Replies - 8727 Views - Last Post: 17 September 2012 - 06:23 AM Rate Topic: -----

#1 Gamegoofs2  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 159
  • Joined: 15-April 09

Extracting Text Between Tags with BeautifulSoup

Posted 08 September 2012 - 06:33 PM

So I've grabbed the specific element I wanted, but I just want the text between the tag. How is this done?

def watcher(url):
	sock = urllib.urlopen(url)
	html = sock.read()
	sock.close()
	
	soup = BeautifulSoup(html)
	
	price = soup.find_all(id="productYoyo_Relative")
	
	
	print price



The documentation on the site has everything, but get text from between a specific element.

This post has been edited by Gamegoofs2: 08 September 2012 - 06:55 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Extracting Text Between Tags with BeautifulSoup

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,534
  • Joined: 05-May 05

Re: Extracting Text Between Tags with BeautifulSoup

Posted 09 September 2012 - 04:17 PM

From what seen I've on the net, there's a way to do it in BeautifulSoup. Looks a little involved though. You could also use Regular Expressions.

This post has been edited by blackcompe: 14 September 2012 - 08:16 AM

Was This Post Helpful? 0
  • +
  • -

#3 charles314  Icon User is offline

  • New D.I.C Head

Reputation: -11
  • View blog
  • Posts: 18
  • Joined: 13-September 12

Re: Extracting Text Between Tags with BeautifulSoup

Posted 13 September 2012 - 08:25 PM

http://docs.python.org/ < best place to get the tech details.
Was This Post Helpful? -1
  • +
  • -

#4 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Extracting Text Between Tags with BeautifulSoup

Posted 14 September 2012 - 07:41 AM

Umm, no. The BeautifulSoup module isn't part of the standard Python library, so the docs on the Python site won't help.

IF the OP was looking for documentation for Beautiful Soup (which he already found) it'd be: http://www.crummy.co...ulSoup/bs4/doc/

@Gamegoofs2 I'm not familiar working with Beautiful Soup but it seems likely that's the sort of thing you can do, I'll have a look at the docs now.
Was This Post Helpful? 1
  • +
  • -

#5 Gamegoofs2  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 159
  • Joined: 15-April 09

Re: Extracting Text Between Tags with BeautifulSoup

Posted 15 September 2012 - 09:11 AM

Thanks Simown.

percent = soup.find_all(id="productYoyo_Relative", text=True)
for item in percent:
    product = str(item)

print product



this seems to return the element as a string, which then will let me do a regex on it. Now my problem seems to be with the regex :P

Thanks for your help guys.
Was This Post Helpful? 0
  • +
  • -

#6 charles314  Icon User is offline

  • New D.I.C Head

Reputation: -11
  • View blog
  • Posts: 18
  • Joined: 13-September 12

Re: Extracting Text Between Tags with BeautifulSoup

Posted 15 September 2012 - 09:18 AM

Docs on the Python site will help, when here it is obvious he does not understand Python. So, obviously, there is no need to go further with this. Learn Logic.
Was This Post Helpful? -4
  • +
  • -

#7 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Extracting Text Between Tags with BeautifulSoup

Posted 15 September 2012 - 10:06 AM

Good job! Post back if you need help with the regex, we can sure help. I see you need help with regex on your other post. I'm on it :)

Actually, all his standard Python seems to be fine even the syntax with the new module, just needed information on the new module which he seems to have found. Personally, I think he does understand Python.

This post has been edited by Simown: 15 September 2012 - 10:08 AM

Was This Post Helpful? 1
  • +
  • -

#8 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Extracting Text Between Tags with BeautifulSoup

Posted 17 September 2012 - 06:23 AM

View Postcharles314, on 15 September 2012 - 12:18 PM, said:

Docs on the Python site will help, when here it is obvious he does not understand Python. So, obviously, there is no need to go further with this. Learn Logic.


The OP clearly DOES know how to write Python code, and like Simown said, Beautiful Soup isn't a standard Python module; so the DOCS won't help. This line price = soup.find_all(id="productYoyo_Relative") actually shows a technique where you can choose what arguments are associated with what parameters. This can be very helpful when all the parameters of a function have default values and you want to pass an argument into the second or third parameter without being forced to supply the ones before it; it can also be nice for the simple fact that you're being explicit rather than implicit (I had a problem at work the other day that would have been averted had I used this technique from the beginning).

EDIT:
THIS ISN'T EVEN A LOGIC ISSUE!!!

This post has been edited by atraub: 17 September 2012 - 06:25 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1