简体   繁体   中英

How to extract html a href Attribute?

I am scraping this page https://en.wikipedia.org/wiki/Lev_Pavlovich_Rapoport

I have come to this point

>>> s2.title.string
u'Lev Pavlovich Rapoport - Wikipedia'
>>> s2.a
<a id="top"></a>
>>> a2=s2.find_all("a")

I will give just couple of lines

[<a id="top"></a>, <a href="#mw-head">navigation</a>, <a href="#p-search">search</a>, <a class="image" href="/wiki/File:LPRapoport1.jpg"><img alt="" class="thumbimage" data-file-height="374" data-file-width="295" height="279" src="//upload.wikimedia.org/wikipedia/commons/thumb/b/bb/LPRapoport1.jpg/220px-LPRapoport1.jpg" srcset="//upload.wikimedia.org/wikipedia/commons/b/bb/LPRapoport1.jpg 1.5x" width="220"/></a>, <a class="internal" href="/wiki/File:LPRapoport1.jpg" title="Enlarge"></a>, <a href="/wiki/Russian_language" title="Russian language">Russian language</a>, <a href="#Early_work"><span class="tocnumber">1</span> <span class="toctext">Early work</span></a>, <a href="#Further_work"><span class="tocnumber">2</span> <span class="toctext">Further work</span></a>, <a href="#Co-workers"><span class="tocnumber">3</span> <span class="toctext">Co-workers</span></a>, <a href="#Recognition"><span class="tocnumber">4</span> <span class="toctext">Recognition</span></a>, <a href="#External_links"><span class="tocnumber">5</span> <span class="toctext">External links</span></a>, <a href=

Now next step would be to take href Attribute,but how?

You can do it like this if you are using BeautifulSoup as I imagine.

for a in s2.find_all('a', href=True):
    print "Found the URL:", a['href']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM