Python and BeautifulSoup not finding 'a'

Here is a snippet of HTML (from delicious):

<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>

I am trying to find all the links where class = "inlinesave action". Here is the code:

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

But finds nothing!

Any thoughts?

thanks

+3
source share
4 answers

If you want to find an anchor with these two classes, you will have to use a regex, I think:

tags = soup.findAll('a', attrs={'class': re.compile(r'\binlinesave\b.*\baction\b')})

Keep in mind that this regular expression will not work if the class name ordering is canceled ( class="action inlinesave").

The following statement should work for all cases (although it looks ugly imo.):

soup.findAll('a', 
    attrs={'class': 
        re.compile(r'\baction\b.*\binlinesave\b|\binlinesave\b.*\baction\b')
    })
+1
source

Python String Methods

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]
0
source

, ​​ verion 3.1.0, ,

>>> html="""<h4>
... <a rel="nofollow" class="taggedlink " href="http://imfy.us/" >Generate Secure Links with Anony
... <span class="saverem">
...   <em class="bookmark-actions">
...     <strong><a class="inlinesave action" href="/save?url=http%3A%2F%2Fimfy.us%2F&amp;title=Gen
...   </em>
... </span>
... </h4>"""
>>>
>>> from BeautifulSoup import BeautifulSoup
>>> soup = BeautifulSoup(html)
>>> tags = soup.findAll('a', attrs={'class':'inlinesave action'})
>>> print len(tags)
1
>>> tags
[<a class="inlinesave action" href="/save?url=http%3A%2F%2Fimfy.us%2F&amp;title=Generate%20Secure%
>>>

BeautifulSoup 2.1.1, .

0

, 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

Gives (a result with a long result is disabled in '...'):

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

Source: https://habr.com/ru/post/1723873/


All Articles