选择单个元素及其值powershell script to python

2024-05-19 10:23:51 发布

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

我在powershell中有这样一个脚本,可以在网页上获取访问者的价值:

$visitors = Invoke-WebRequest -Uri https://footballarena.org
$visitors = $visitors.AllElements | where class -EQ "right" | select -ExpandProperty innertext
$visitors = $visitors -replace '\D+'
$visitors | Export-Csv $env:USERPROFILE\Desktop\export.txt

输出只是单个DIV类“right”的数值

现在我需要用python编写相同的脚本。 我可以阅读并存储页面:

^{pr2}$

现在我需要从这个类中选择值“161”:

<div class="right">161 online</div>

我发现了这个问题,但我不知道如何使用它-Python Selenium selecting div class

谁能帮忙吗?在


Tags: httpsorgrightdiv脚本网页uriclass
1条回答
网友
1楼 · 发布于 2024-05-19 10:23:51

可以使用BeautifulSoup完成此操作,使用^{cd1>}安装,然后类似于:

from bs4 import BeautifulSoup
import urllib.request

myurl = "https://footballarena.org"

html_doc = urllib.request.urlopen(myurl).read()

soup = BeautifulSoup(html_doc, 'html.parser')

result = soup.findAll("div", { "class" : "right" })

print(result[0].text.split()[0])

输出:

^{pr2}$

也许可以改进,但这是一个普遍的想法。希望它能帮上忙。

相关问题 更多 >

    热门问题