2017-02-10 34 views
0

我想提取从网站上的一些价值观和特定元素掌握空,则返回Python的网络刮

<div class="float_l dcMsg"> 
    <div class="float_l" style="margin-right: 5px; min-width: 105px;">Slow Stochastic(20,5)</div> 
    <div class="float_l ind-color-box" style="margin-right: 5px; background: rgb(242, 38, 31);"></div> 
    <div class="float_l" style="margin-right: 5px; min-width: 105px;">%K: 33.996</div> 
    <div class="float_l ind-color-box" style="margin-right: 5px; background: rgb(0, 255, 0);"></div> 
    <div class="float_l" style="margin-right: 5px; min-width: 105px;">%D: 18.393</div> 
</div> 

我想要的值是4号线(即33.996)和6号线(即18.393)。

我想要的这些数字实际上来自动态图表,但我不知道它是否来自javascript。 按下网页上的某个按钮后,数字将更新为最新值,并且元素中的数字值也会相应更改。此外,当我将鼠标悬停在图表上时,数字将会改变。

但是,网页将不会被重新加载,但只有在按下按钮后,页面元素的数字部分才会被更改。

我试过这段代码,但它返回[]。

import urllib 
import re 

htmltext = urllib.urlopen("http://www.example.com").read() 

regex = '<div class="float_l" style="margin-right: 5px; min-width: 105px;">(.+?)</div>' 

pattern = re.compile(regex) 

results = re.findall(pattern,htmltext) 

print results 

我也尝试过使用BeautifulSoup,但它也返回[]。

import bs4 as bs 
import urllib 

sauce = urllib.urlopen('http://www.example.com').read() 

soup = bs.BeautifulSoup(sauce,'html.parser') 

results = soup.findAll('div',style='margin-right: 5px; min-width: 105px;') 

print results 
+2

总部设在你给的信息,你* *威力要使用[硒(http://stackoverflow.com/questions/17540971/how-to-use -selenium与 - 蟒)。 BeautifulSoup不处理动态加载或更改的值 – Wondercricket

+0

Ha! ....我没有任何有用的贡献,但我需要打招呼 – Kelvin

回答

0

硒可能是一个很好的组合,但它是可行的。

也许是这样的:

In [30]: for el in soup.findAll('div'): 
    ...:  if el.has_attr('style') and 'margin-right: 5px' in el.attrs['style'] and el.attrs['class'] == ['float_l']: 
    ...:   print el 
    ...: 
    ...: 
<div class="float_l" style="margin-right: 5px; min-width: 105px;">Slow Stochastic(20,5)</div> 
<div class="float_l" style="margin-right: 5px; min-width: 105px;">%K: 33.996</div> 
<div class="float_l" style="margin-right: 5px; min-width: 105px;">%D: 18.393</div> 
+0

这不会返回我想要的。我想我会尝试使用硒,并感谢您的帮助。 – kelvin