我尝试使用选择器小工具获取元素(页面中的编号591),选择器小工具找到“#e0 .inline:nth-child(1)” 但这不起作用
r = requests.get('http://www.tsetmc.com/loader.aspx?ParTree=151311&i=42031056662310763')
soup = BeautifulSoup(r.content, 'html.parser')
soup.select('#e0 .inline:nth-child(1)')
如何通过选择器小工具或其他方式获取此元素?
答案 0 :(得分:1)
值591
是从外部URL加载的,您可以尝试使用以下脚本来加载它:
import re
import requests
from bs4 import BeautifulSoup
url = 'http://www.tsetmc.com/loader.aspx?ParTree=151311&i=42031056662310763'
html_data = requests.get(url).text
csecval = re.search(r"CSecVal='(\d+)", html_data).group(1)
i = re.search(r'i=(\d+)', url).group(1)
d = []
while not d:
data = requests.get('http://www.tsetmc.com/tsev2/data/instinfodata.aspx', params={'i': i, 'c': csecval}).text
d = data.split(';')[4]
print(d.split(',')[0])
打印:
591