很抱歉这个菜鸟html抓取问题,但我正在处理复杂的html,每种情况都是独一无二的。
我正在尝试解析出前面有:{“actionType”:“导航”,“actionUrl”的所有URL:
在下面的示例中,它将是https://www.ABCD.com
我使用的是python。最好是很美的汤。关于如何接近的想法?
</a>
<a aria-label="ABCD." class="we-lockup targeted-link l-column small-2 medium-3 large-2 we-lockup--shelf-align-top ember-view" data-metrics-click='{"actionType":"navigate","actionUrl":"https://www.ABCD.com","targetType":"card","targetId":"12345"}' data-metrics-location='{"locationType":"shelfCustomersAlsoBoughtMovie"}' href="https://www.ABCD.com" id="ember123"> <picture class="we-lockup__artwork we-artwork--lockup we-artwork--fullwidth we-artwork--vhs-movie-pic we-artwork ember-view" dir="ltr" id="ember123">
<noscript>发布于 2020-12-30 09:34:28
您可以使用内置的json模块将数据转换为Python字典(dict),然后访问actionUrl密钥。
import json
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, "html.parser")
data = soup.find(
class_=
'we-lockup targeted-link l-column small-2 medium-3 large-2 we-lockup--shelf-align-top ember-view'
)['data-metrics-click']
json_data = json.loads(data)
print(type(json_data))
print(json_data['actionUrl'])输出:
<class 'dict'>
https://www.ABCD.comhttps://stackoverflow.com/questions/65500320
复制相似问题