假设我想从网站上搜集一些元数据:
https://www.diepresse.com/4913597/autocluster-buhlt-um-osterreich-teststrecke-fur-google-autos
更准确地说,即从键fullChannel
中,从这个<script>
标记中得到值/home/wirtschaft/international
:
<script>
let pageBreakpoint = 'desktop';
let _screen = window.innerWidth;
if (_screen < 640) {
pageBreakpoint = 'mobile';
} else if (_screen < 1024) {
pageBreakpoint = 'tablet';
}
var dataLayer = window.dataLayer || [];
dataLayer.push({
'siteId': 'dpo',
'contentId': '4913597',
'pageType': 'article',
'contentTitle': 'Autocluster buhlt um Österreich-Teststrecke für Google-Autos',
'contentAuthor': '',
'contentElements': '',
'contentType': 'default',
'pageTags': '',
'wordCount': '264',
'wordCountRounded': '400',
'contentSource': '',
'contentPublishingDate': '',
'contentPublishingDateFormat': '28/01/2016',
'contentPublishingTime': '08:52',
'contentPublishingTimestamp': '28/01/2016 08:52:00',
'contentRepublishingTimestamp': '28/01/2016 08:52:00',
'contentTemplate': 'default',
'metaCategory': '',
'channel': 'international',
'fullChannel': '/home/wirtschaft/international',
'canonicalUrl': '',
'fullUrl': window.location.href,
'oewaPath': 'RedCont/Wirtschaft/Wirtschaftspolitik',
'oewaPage': 'homepage',
'isPremium':'no',
'isPremiumArticle': 'free',
'pageBreakpoint': pageBreakpoint,
'userId': ''
});
</script>
现在我正在使用Selenium和Xpath,但我真的不知道如何在这方面使用正则表达式:
#this doesnt work
driver.find_element_by_xpath("//script[text()]")
有什么建议吗?你知道吗
使用JavaScript Executor获取var值
datalayer
。它将作为json数组返回。你知道吗然后得到键
fullChannel
的值。你知道吗输出:
键值
fullChannel
您的XPath查找脚本似乎是错误的,请尝试以下操作:
然后,可以使用一些字符串解析方法从
fullChannel
中提取值:这将产生输出
'/home/wirtschaft/international'
可能有比硒更有效的方法,但我会把硒的答案留在这里,以防你想走这条路。你知道吗
相关问题 更多 >
编程相关推荐