python中的selenium webdriver(selenium webdriver in python)

编程入门 行业动态 更新时间:2024-10-22 21:21:26
python中的selenium webdriver(selenium webdriver in python)

所以基本上我试图在python中创建脚本,使用selenium webdriver打开并登录一个名为marketsworlds的网站并检索股票的市场价值。 我有脚本能够打开并登录到页面。 我只是想弄清楚如何捕获/获得市场价值并将其打印出来。 我使用inspect元素来查找类:

<p class="market_value"> </p>

在开放和关闭括号之间检查元素显示市场价值,其不断变化。 我尝试将driver.find_element_by_class(“market_value”)设置为变量并打印变量。 我得到了一个“在对象0x”打印出来的东西以及x之后的东西。 任何方式返回它实际显示的内容?

So basically I'm trying to make script in python that uses selenium webdriver to open and login to a website called marketsworlds and retrieve the market value of a stock. I have the script able to open and login into the page. I just cant figure out how to capture/get the market value and have it print out. I used inspect element to find the class:

<p class="market_value"> </p>

in between the open and close brackets inspect element displays the market value, which constantly changes. I tried setting driver.find_element_by_class("market_value") to a variable and printing the variable. I get a print out of " at object 0x" and what ever comes after the x. any way to return what it actually displays?

最满意答案

如果你必须使用Selenium进行导航,例如在JavaScript密集的网站上,它会建议获取页面源并使用HTML解析器来提取所需的数据。

BeautifulSoup是解析器的绝佳选择。 例如:

html = driver.page_source soup = BeautifulSoup(html) # Get *all* 'p' tags with the specified class attribute. p_tags = soup.findAll('p',{'class':'market_value'}) for p in p_tags: print p.text

这应打印以使用类market_value <p>标记中包含的market_value 。 但是,如果不知道确切的页面来源,很难给出细节。

但是,如果您决定严格使用Selenium,您可以通过以下方式找到这些元素:

# Get *all* 'p' tags with the specified class attribute. elements = driver.find_elements_by_class_name('market_value') for element in elements: print element.text # or # Get a *single* 'p' tag with the specified class attribute. element = driver.find_element_by_class_name('market_value') print element.text

If you have to use Selenium for navigation, such as on JavaScript-heavy sites, It would suggest acquiring the page source and using an HTML parser to extract the data you want.

BeautifulSoup is a great choice of parser. For example:

html = driver.page_source soup = BeautifulSoup(html) # Get *all* 'p' tags with the specified class attribute. p_tags = soup.findAll('p',{'class':'market_value'}) for p in p_tags: print p.text

This should print to screen the text contained in the <p> tags with the class market_value. Its hard to give specifics without knowing the exact page source, however.

However, if you're determined to use strictly Selenium, you can find these elements by:

# Get *all* 'p' tags with the specified class attribute. elements = driver.find_elements_by_class_name('market_value') for element in elements: print element.text # or # Get a *single* 'p' tag with the specified class attribute. element = driver.find_element_by_class_name('market_value') print element.text

更多推荐

本文发布于:2023-08-01 10:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1357958.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:python   selenium   webdriver

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!