2017-02-19 75 views
0

我有以下內容的字符串:解析字符串得到HREF定的屬性值

var string = 
'<div class="product-info-inner-content clearfix ">\ 
    <a href="http://www.adidas.co.uk/ace-17_-purecontrol-firm-ground-boots/BB4314.html"\ 
     class="link-BB4314 product-link clearfix "\ 
     data-context="name:ACE 17+ Purecontrol Firm Ground Boots"\ 
     data-track="BB4314"\ 
     data-productname="ACE 17+ Purecontrol Firm Ground Boots" tabindex="-1">\ 
     <span class="title">ACE 17+ Purecontrol Firm Ground Boots</span>\ 
     <span class="subtitle">Men Football</span>\ 
    </a>\ 
</div>'; 

我想執行的JavaScript相當於下面的Python代碼,在使用美湯搶網址給出產品代碼的div類元素(即本例中爲BB4314)。

is_listing = len(soup.findAll(name="div", attrs={"class": "product-tile"})) > 1 
     if is_listing: 
     # stuck from this part 
     attrs = {"class": re.compile(r".*\bproduct-link\b.*"), "data-track": code} 
     url = soup.find(name="a", attrs=attrs) 
     url = url["href"] 

我該怎麼做?

回答

2

只需使用DOM

var string = '<div class="product-info-inner-content clearfix "><a href="http://www.adidas.co.uk/ace-17_-purecontrol-firm-ground-boots/BB4314.html" class="link-BB4314 product-link clearfix " data-context="name:ACE 17+ Purecontrol Firm Ground Boots" data-track="BB4314" data-productname="ACE 17+ Purecontrol Firm Ground Boots" tabindex="-1"><span class="title">ACE 17+ Purecontrol Firm Ground Boots</span> <span class="subtitle">Men Football</span></a></div>', 
 
    div = document.createElement("div"); 
 
div.innerHTML = string; 
 
var href = div.querySelector("a.product-link").href, 
 
    parts = href.split("/"), 
 
    code = parts.pop().split(".")[0]; 
 
console.log(code) 
 

 
console.log(div.querySelector("a.product-link").getAttribute("data-track"))

+0

感謝。如果字符串看起來像這樣:http://pastebin.com/ttpn6wth(即解析整個網頁),這會起作用嗎? – methuselah

+0

是的,但你也可以得到和解析meta標籤:'' – mplungjan

+0

感謝,但meta標籤將我帶到了列表頁面,實際上,我需要產品頁面。 – methuselah