Roland Smith
Roland Smith

Reputation: 51

Python: Print Specific line of text out of TD tag

This is an easy one I am sure. I am parsing a website and I am trying to get the specific text in between tags. The text will either == [revoked, Active, Default] I am using Python. I have been able to print out all the inner text results, but I have not been able to find a good solution on the web for specific text. Here is my code

from BeautifulSoup import BeautifulSoup
import urllib2
import re

url = urllib2.urlopen("Some URL")
content = url.read()
soup = BeautifulSoup(content)
for tag in soup.findAll(re.compile("^a")):

print(tag.text)

Upvotes: 0

Views: 512

Answers (2)

Ehsan Iran-Nejad
Ehsan Iran-Nejad

Reputation: 1807

I've used the snippet below in a similar occasion. See if this works with your goal:

table = soup.find(id="Table3")

for i in table.stripped_strings:
    print(i)

Upvotes: 0

kirelagin
kirelagin

Reputation: 13616

I'm still not sure I understand what you are trying to do, but I'll try to help.

soup.find_all('a', text=['revoked', 'active', 'default'])

This will select only those <a …> tags that have one of given strings as their text.

Upvotes: 1

Related Questions