从 beautifulsoup 中的 id 中提取文本
extracting text from id in beautifulsoup
有html这样的代码。使用 BeautifulSoup,我想提取 2,441
的文本
有一个 span 元素和一个等于 lastPrice 的 id。
<span id="lastPrice">2,441.00</span>
我已经尝试在网上查找并解决,但我仍然无法解决。我是初学者。
i have tried this:
tag = soup.span
price = soup.find(id="lastPrice")
print(price.text)
试试这个:
price = soup.select("#lastPrice")[0]
print(price.text)
不是 bs4,而是正则表达式如下:
import re
line = '<span id="lastPrice">2,441.00</span>'
print(p.sub("", data))
您在页面上看到的数据是通过 JavaScript 呈现的,因此 BeautifulSoup 看不到价格。价格以 JavaScript 形式嵌入页面中。您可以提取它,例如:
import json
import requests
from bs4 import BeautifulSoup
url = "https://www1.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuote.jsp?symbol=HDFC"
headers = {
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0"
}
soup = BeautifulSoup(requests.get(url, headers=headers).content, "html.parser")
data = json.loads(soup.find(id="responseDiv").contents[0])
# uncomment this to print all data:
# print(json.dumps(data, indent=4))
print(data["data"][0]["lastPrice"])
打印:
2,407.90
有html这样的代码。使用 BeautifulSoup,我想提取 2,441
的文本有一个 span 元素和一个等于 lastPrice 的 id。
<span id="lastPrice">2,441.00</span>
我已经尝试在网上查找并解决,但我仍然无法解决。我是初学者。
i have tried this:
tag = soup.span
price = soup.find(id="lastPrice")
print(price.text)
试试这个:
price = soup.select("#lastPrice")[0]
print(price.text)
不是 bs4,而是正则表达式如下:
import re
line = '<span id="lastPrice">2,441.00</span>'
print(p.sub("", data))
您在页面上看到的数据是通过 JavaScript 呈现的,因此 BeautifulSoup 看不到价格。价格以 JavaScript 形式嵌入页面中。您可以提取它,例如:
import json
import requests
from bs4 import BeautifulSoup
url = "https://www1.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuote.jsp?symbol=HDFC"
headers = {
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0"
}
soup = BeautifulSoup(requests.get(url, headers=headers).content, "html.parser")
data = json.loads(soup.find(id="responseDiv").contents[0])
# uncomment this to print all data:
# print(json.dumps(data, indent=4))
print(data["data"][0]["lastPrice"])
打印:
2,407.90