[英]How to match an exact word in a json soup?
我正在解析从网址中'PatientID'
患者元数据,并且试图访问'PatientID'
字段。 但是,还有一个'OtherPatientIDs'
字段,该字段由我的搜索获取。
我曾尝试使用正则表达式,但不清楚如何匹配EXACT字符串或如何将其合并到我的代码中。
因此,目前,我已经完成了:
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
PatientID = "PatientID"
lines = soup.decode('utf8').split("\n")
for line in lines:
if "PatientID" in line:
PatientID = line.split(':')[1].split('\"')[1].split('\"')[0]
print(PatientID)
它可以成功找到PatientID和OtherPatientIDs字段的值。 如何指定我只需要PatientID字段?
编辑:我被要求给出一个例子,我得到了response.text,它的形式为:
{
"ID" : "shqowihdojcoughwoeh"
"LastUpdate: "20190507"
"MainTags" : {
"OtherPatientIDs" : "0304992098"
"PatientBirthDate" : "29/04/1803"
"PatientID" : "92879837"
"PatientName" : "LASTNAME^FIRSTNAME"
},
"Type" : "Patient"
}
为什么不使用json
库呢?
import json
import requests
response = requests.get(url)
data = json.loads(response.text)
print(data['MainTags']['PatientID'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.