性能仪表中的Web刮取数据

2024-05-14 06:06:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用python和请求html库在小部件中提取数据

我想要的值在一个带有箭头的仪表中,箭头指向五个可能的结果。 仪表上的每个标签在网站的所有页面上都是相同的。我面临的问题是我不能在仪表标签上使用css选择器来提取文本,我需要提取箭头本身的值,因为它将指向标签。箭头没有文本属性,因此如果我使用css选择器,则不会得到任何响应

每个箭头都有一个唯一的类名

<div class="arrow-F-uE7IX8 arrowToStrongBuy-1ydGKDOo arrowStrongBuyShudder-3xsGK8k5">
https://www.tradingview.com/symbols/NASDAQ-MDB/
StrongBuy:

<div class="arrow-F-uE7IX8 arrowToBuy-1R7d8UMJ arrowBuyShudder-3GMCnG5u">
https://www.tradingview.com/symbols/NYSE-XOM/
Buy:

<div class="arrow-F-uE7IX8 arrowToStrongSell-3UWimXJs arrowStrongSellShudder-2UJhm0_C">
https://www.tradingview.com/symbols/NASDAQ-IDEX/ 
StrongSell:

如何确保获得正确的值?我不确定如何检查选择器是否包含arrowTo{foo}并将其存储为变量

import pyppdf.patch_pyppeteer
from requests_html import AsyncHTMLSession
asession = AsyncHTMLSession()


async def get_page():
    code = 'NASDAQ-MDB'
    r = await asession.get(f'https://www.tradingview.com/symbols/{code}/')
    await r.html.arender(wait=3)
    return r

results = asession.run(get_page)
for result in results:

    arrow_class_placeholder = "//div[contains(@class,'arrow-F-uE7IX8 arrowToStrongBuy-1ydGKDOo')]//div[1]"
    arrow_class_name = result.html.xpath(arrow_class_placeholder,first=True)

    if arrow_class_name == "//div[contains(@class,'arrow-F-uE7IX8 arrowToStrongBuy-1ydGKDOo')]//div[1]":
        print('StrongBuy')
    else:
        print('not strong buy')

Tags: httpsdivcomhtmlwww仪表选择器标签
1条回答
网友
1楼 · 发布于 2024-05-14 06:06:13

您可以使用BeautifulSoup4 (bs4),这是一个Python库,用于从HTML和XML文件中提取数据,并结合使用Regular Expressions (RegEx)。在本例中,我使用python re库用于正则表达式

这是您想要的(source):

enter image description here

在上面的示例中soup.find_all(class_=re.compile("itle"))返回在类标记中找到单词“itle”的所有实例,例如下面显示的html文档中的class = "title"

enter image description here

对于您的正则表达式,它看起来像"arrowTo*",甚至只是"arrowTo"soup.find_all(class_=re.compile("arrowTo"))

您的最终代码应该如下所示:

from bs4 import BeautifulSoup
#i think result was your html document from requests library
#the first parameter is your html document variable
soup = BeautifulSoup(result, 'html.parser') 
myArrowToList = soup.find_all(class_=re.compile("arrowTo"))

如果您想要"arrowToStrongBuy",只需在find_all函数的正则表达式输入中使用它

soup.find_all(class_=re.compile("arrowToStrongBuy"))

相关问题 更多 >