Python e BeautifulSoup, non trovando 'a'


Answers

Metodi di stringa Python

html=open("file").read()
for item in html.split("<strong>"):
    if "class" in item and "inlinesave action" in item:
        url_with_junk = item.split('href="')[1]
        m = url_with_junk.index('">') 
        print url_with_junk[:m]
Question

Ecco un pezzo di codice HTML (delizioso):

<h4>
<a rel="nofollow" class="taggedlink " href="http://imfy.us/" >Generate Secure Links with Anonymous Referers &amp; Anti-Bot Protection</a>
<span class="saverem">
  <em class="bookmark-actions">
    <strong><a class="inlinesave action" href="/save?url=http%3A%2F%2Fimfy.us%2F&amp;title=Generate%20Secure%20Links%20with%20Anonymous%20Referers%20%26%20Anti-Bot%20Protection&amp;jump=%2Fdux&amp;key=fFS4QzJW2lBf4gAtcrbuekRQfTY-&amp;original_user=dux&amp;copyuser=dux&amp;copytags=web+apps+url+security+generator+shortener+anonymous+links">SAVE</a></strong>
  </em>
</span>
</h4>

Sto cercando di trovare tutti i link in cui class = "inlinesave action". Ecco il codice:

sock = urllib2.urlopen('http://delicious.com/theuser')
html = sock.read()
soup = BeautifulSoup(html)
tags = soup.findAll('a', attrs={'class':'inlinesave action'})
print len(tags)

Ma non trova nulla!

qualche idea?

Grazie




Potresti avanzare in qualche modo usando il pyparsing:

from pyparsing import makeHTMLTags, withAttribute

htmlsrc="""<h4>... etc."""

atag = makeHTMLTags("a")[0]
atag.setParseAction(withAttribute(("class","inlinesave action")))

for result in atag.searchString(htmlsrc):
    print result.href

Fornisce (output di risultati lunghi tagliato a "..."):

/save?url=http%3A%2F%2Fimfy.us%2F&amp;title=Genera...+anonymous+links