2017-03-09 139 views
0
<a id="ember1601" role="button" href="/carsearch/book?piid=AQAQAQRRg2INmYAyjZmAMwmKOGATj2qoYBQANIAVCeAZgB6fUEsAED&amp;totalPriceShown=71.66&amp;searchKey=-575257062&amp;offerQualifiers=GreatDeal" data-book-button="book-EY-EC-Car" target="_self" class="ember-view btn btn-secondary btn-action"><span class="btn-label"> 
    <span aria-hidden="true"> 
     <span class="visuallyhidden"> 
      Reserve Item 1, Economy from Economy Rent a Car Rental Company at $72 total 
    </span>Reserve 
    </span> 

</span> 
</a> 

您好,我是新來的蟒蛇 我無法得到的價格& 72下<span class="visuallyhidden">,還我怎樣才能在標籤<a>的href鏈接第一行,請幫助,順便說一句,謝謝 ,我使用美麗的庫,如果其他庫可以幫助,請讓我知道。感謝Python的Beautifulsoup不能從標籤內容有隱藏屬性

回答

1
In [9]: soup = BeautifulSoup(html, 'lxml') # html is the code you posted 

In [10]: soup.find("span", class_="visuallyhidden").text 
Out[10]: '\n   Reserve Item 1, Economy from Economy Rent a Car Rental Company at $72 total\n ' 

In [11]: soup.a["href"] 
Out[11]: '/carsearch/book?piid=AQAQAQRRg2INmYAyjZmAMwmKOGATj2qoYBQANIAVCeAZgB6fUEsAED&totalPriceShown=71.66&searchKey=-575257062&offerQualifiers=GreatDeal' 

如果你需要從字符串中提取一部分文本,你需要使用正則表達式:

In [12]: text = soup.find("span", class_="visuallyhidden").text 

In [15]: re.search(r'\$\d+', text).group() 
Out[15]: '$72' 
+0

感謝您的回覆,但無法看到文字,可能是網站的block crawler?請幫助檢查網站:https://www.orbitz.com/carsearch?date1=03%2F08%2F2017&date2=3%2F09%2F2017&loc2=lax&locn=lax&rdus=10&selCC=%5B%22economy%22%5D&vend= –

0

beautifulsoup能夠通過它的類名找到一個標籤一樣,

bs_obj = BeautifulSoup(html) 
tag = bs_obj.find("span", class_ = "visuallyhidden") # string "class" is reserved for python itself,so bs use string "class_" 
s = tag.string # that will get string inside the span 
... 
# you can get "$72" by regx 

另外,BS允許您通過 「[]」 operator.Just訪問標籤的ATTR像

print(tag['href']) 

您可以在線查看bs doc中的一些簡單示例。

+0

謝謝爲您的回覆,但無法獲取文本,可能是網站的爬蟲?請幫助檢查網站:https://www.orbitz.com/carsearch?date1=03%2F08%2F2017&date2=3%2F09%2F2017&loc2=lax&locn=lax&rdus=10&selCC=%5B%22economy%22%5D&vend=謝謝 –