Py学习  »  Python

使用python进行web抓取-获取属性值时出现问题[duplicate]

Monkeydave • 5 年前 • 1646 次点击  

我对网络垃圾很陌生,我正努力从特定元素中获取两个属性的值

我想找到 data-diffusion-decimal 和; data-diffusion-history

enter image description here

soup.findAll('div',attrs={"class":"RC-runnerPriceWrapper"})

我得到的是:

<div class="RC-runnerPriceWrapper PC-bestOddsContainer js-diffusionHorsesList js-horsesList js-bestOddsPriceContainer" data-diffusion-horsename="Dinons">  <a class="ui-btn RC-runnerPrice ui-priceBtn_noPrice js-diffusionPriceValue js-betHandler js-runnerPrice js-runnerPriceBestOdds" data-test-selector="RC-cardPage-runnerPrice" href="#"></a>

这是我所能得到的,但我所需要的并不包含在结果中。非常感谢任何建议

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/44971
 
1646 次点击  
文章 [ 2 ]  |  最新文章 5 年前
Andersson Jeppe Spanggaard
Reply   •   1 楼
Andersson Jeppe Spanggaard    6 年前

用硒之类的

driver.find_element_by_css_selector('div.RC-runnerPriceWrapper').get_attribute('data-diffusion-decimal')
Corentin Limier
Reply   •   2 楼
Corentin Limier    6 年前

也许这些属性是在javascript中动态设置的。要知道这一点,请不要使用控制台,而是右键单击页面,然后单击“查看页面源”。

如果您在源代码中找不到这些属性,它们是用javascript设置的,您需要一个类似于 Selenium 执行页面的动态部分。

解决方法:使用浏览器控制台的“网络”选项卡,可以尝试查看是否执行了ajax请求以获取属性中的数据。不用解析页面,您可以调用相同的请求,也许可以获得json格式的信息。