gpt4 book ai didi

python - 从html中提取属性值

转载 作者:行者123 更新时间:2023-12-01 03:53:55 25 4
gpt4 key购买 nike

import requests
from bs4 import BeautifulSoup

r = requests.get("http://www.nepalstock.com.np/marketdepthofcompany/238")
soup = BeautifulSoup(r.content,"lxml")
value_select = soup.select_one("select.form-control")
for val in value_select.find("option")[1:]:
n = val['value']
print (n)

为什么上面的代码打印出html中的文本而不是属性值?

最佳答案

因为find返回单个标签,所以您需要find_all:

for val in value_select.find_all("option")[1:]:
n = val['value']
print (n)

或者使用 css 选择器跳过第一个选项:

for val in value_select.select("option + option"):

两者的工作原理相同:

In [1]: import requests

In [2]: from bs4 import BeautifulSoup

In [3]: r = requests.get("http://www.nepalstock.com.np/marketdepthofcompany/238")

In [4]: soup = BeautifulSoup(r.content,"lxml")

In [5]: value_select = soup.select_one("select.form-control")

In [6]: for val in value_select.find_all("option")[1:]:
...: n = val['value']
...: print (n)
...:
ACEDBL
ACEDPO
ADBL
AHPC
ALDBL
ALDBLP
ALICL
ALICLP
APEX
APEXPO
API
ARDBL
ARDBLP
ARUN
ARUNPO
AVU
BARUN
BBBLNP
...................................

In [7]: for val in value_select.select("option + option"):
...: n = val['value']
...: print (n)
...:
ACEDBL
ACEDPO
ADBL
AHPC
ALDBL
ALDBLP
ALICL
ALICLP
APEX
APEXPO
API
ARDBL
ARDBLP
ARUN
ARUNPO
AVU
BARUN
BBBLNP
..........................

要获取值为整数的选项,请选择 id StockSymbol_Select2,有多个 select.form-control,因此您需要准确指定哪一个:

import requests
from bs4 import BeautifulSoup

r = requests.get("http://www.nepalstock.com.np/marketdepthofcompany/238")
soup = BeautifulSoup(r.content,"lxml")
value_select = soup.select_one("#StockSymbol_Select2")
for val in value_select.select("option + option"):
print (val["value"])

这会给你你想要的:

In [13]: value_select = soup.select_one("#StockSymbol_Select2")

In [14]: for val in value_select.select("option + option"):
....: print (val["value"])
....:
216
294
397
360
406
660
385
599
262
666
697
..............................

关于python - 从html中提取属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37830389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com