在标签内找到文本

时间:2019-07-19 09:57:19

标签: python beautifulsoup

我想不出一种方法来使用“ td”标签和“ fc-bi-regcode-value”类在元素内获取文本 这是html的样子:

<td class="fc-bi-regcode-value">74000234</td>

想要74000234代码 谁能帮忙

soup=BeautifulSoup(requests.get(url).text,"html.parser")
xx=soup.find("td",class_="fc-bi-regcode-value")
regcode.append(xx.text)

2 个答案:

答案 0 :(得分:0)

该表在页面上不存在,它是通过AJAX请求从以下网址加载的:https://www.neti.ee/api/BusinessDirectory/get?ixRegCode=74000234

您需要自己发出请求并使用JSON

import requests
from pprint import pprint

url = 'https://www.neti.ee/api/BusinessDirectory/get?ixRegCode=74000234'
data = requests.get(url).json()
reg_code = data['org']['regcode']
pprint(reg_code)

输出:

'74000234'

答案 1 :(得分:0)

您可以使用以json()格式返回结果的API。

headers = {'User-Agent':
       'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36'}
rseponse=requests.get("https://www.neti.ee/api/BusinessDirectory/get?ixRegCode=74000234",headers=headers).json()
print(rseponse['org']['regcode'])