简体   繁体   English

如何匹配json汤中的确切单词?

[英]How to match an exact word in a json soup?

I am parsing through Patient Metadata scraped from a url, and I am trying to access the 'PatientID' field. 我正在解析从网址中'PatientID'患者元数据,并且试图访问'PatientID'字段。 However, there is also an 'OtherPatientIDs' field, which is grabbed by my search. 但是,还有一个'OtherPatientIDs'字段,该字段由我的搜索获取。

I have tried looking into using regular expressions but I am unclear on how to match an EXACT string or how to incorporate it into my code. 我曾尝试使用正则表达式,但不清楚如何匹配EXACT字符串或如何将其合并到我的代码中。

So at the moment, I have done: 因此,目前,我已经完成了:

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)

Which successfully finds the values of both the PatientID AND OtherPatientIDs field. 它可以成功找到PatientID和OtherPatientIDs字段的值。 How do I specify that I only want the PatientID field? 如何指定我只需要PatientID字段?

EDIT: I was asked to give an example of what I get with response.text, and it's of the form: 编辑:我被要求给出一个例子,我得到了response.text,它的形式为:

{
    "ID" : "shqowihdojcoughwoeh"
    "LastUpdate: "20190507"
    "MainTags" : {
         "OtherPatientIDs" : "0304992098"
         "PatientBirthDate" : "29/04/1803"
         "PatientID" : "92879837"
         "PatientName" : "LASTNAME^FIRSTNAME"
     },
     "Type" : "Patient"
}

Why not use the json library instead? 为什么不使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM