J.Liu J.Liu - 1 month ago 15
HTML Question

<Web Scraping python> Extract data in class href tag

I want to use python / beautifulsoup scrap this site :
http://www.ipeen.com.tw/search/all/000/0-100-0-0/%E4%B8%AD%E5%BC%8F/?adkw=%E5%8F%B0%E5%8C%97

Main question is how to extract "/shop/1057970-麻辣公館"
from html like this :



<h3 class="name" id="shop_h3_">
<a class="a37 ga_tracking" data-action="ad_shop" data-category="search" data-label="店名" href="/shop/1057970-麻辣公館" target="_blank">麻辣公館</a>
<span class="markVip">合作店家 <a class="joinVip ga_tracking" data-action="ad_shop" data-category="search" data-label="如何刊登" href="http://www.ipeen.com.tw/ad/adipeen.php?id=b31149405ef268fe0b2dd035f450000e">(如何刊登?)</a></span> </h3>
<div class="serPic">
<div class="align">
<span></span>
<a href="/shop/1057970-麻辣公館" target="_blank"><img alt="麻辣公館" class="lazy" src="http://iphoto.ipeen.com.tw/photo/ipeen/140x140/vip/0/7/9/store_1057970/sp1057970_20160416132749762.jpg" title="麻辣公館"/></a>
</div>
</div>





The one below is my code :



url ='http://www.ipeen.com.tw/search/all/000/0-100-0-0/%E4%B8%AD%E5%BC%8F/?adkw=%E5%8F%B0%E5%8C%97'
opener=urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
page = opener.open(url)
soup = BeautifulSoup(page)
soup.find_all('a', {'class':"a37 ga_tracking"})





Please give some possible ideas
Thanks a lot !

Answer

You were on a good road. Just need to use:

soup.find('a', {'class':"a37 ga_tracking"})['href']

or, if You want more than one link:

[tag['href'] for tag in soup.find_all('a', {'class':"a37 ga_tracking"})]
Comments