1

Using beautfiulsoup to parse sourcecode for scraping:

tempSite = preSite+'/contact_us/'
print tempSite
theTempSite = urlopen(tempSite).read()
currentTempSite = BeautifulSoup(theTempSite)
lightwaveEmail = currentTempSite('input')[7]

#<input type="Hidden" name="bb_recipient" value="[email protected]" />

How can I re.compile lightwaveEmail so that only [email protected] is printed?

2 Answers 2

2

Kinda going about it the wrong way. The reason its the wrong way is that you're using numbered indexes to find the tag you want - BeautifulSoup will find tags for you based on their tag, or attributes which makes it a lot simpler.

You want something like

tempSite = preSite+'/contact_us/'
print tempSite
theTempSite = urlopen(tempSite).read()
soup = BeautifulSoup(theTempSite)
tag = soup.find("input", { "name" : "bb_recipient" })
print tag['value']
Sign up to request clarification or add additional context in comments.

5 Comments

tagging and attributing helps tremendously, however I'm still not quite getting the output I'm looking for: In this case it's now printing [<input type="Hidden" name="bb_recipient" value="[email protected]" />] - I want to explicitly splice the value field as the only output.
.findAll() returns a list. Use soup.find('input', dict(name="bb_recipient", value=True)) to get the first found element.
@J.F.Sebastian - Good catch, modified my answer.
value=True in the previous comment selects <input> that has value attribute to avoid exception on tag['value'].
Awesome. Great examples and helped me understand.
0

If the question is how to get the value attribute from the tag object, then you can use it as a dictionary:

lightwaveEmail['value']

You can find more information about this in the BeautifulSoup documentation.

If the question is how to find in the soup all input tags with such a value, then you can look for them as follows:

soup.findAll('input', value=re.compile(r'[email protected]'))

You can find a similar example also in the BeautifulSoup documentation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.