简体   繁体   中英

Extract words from text files with python

I have an html file with content I can't extract easily with BeautifulSoup because I think it is loaded with Javascript.

..."inlineParams":"json","title":"","lNameP":"MYNAME","key":"degree_result_person"},"firstName":"MYFIRSTNAME"...

I have multiples names in this file that I would like to extract. Those names are just after "lNameP". Is there any way to do a loop to get all those names (in this case i would like to get MYNAME) ?

Thanks a lot,

Using regex?

import re
pattern = re.compile('\"(lNameP)\"\:\"(.*?)\"')
result = pattern.findall(string)

result[0][0] would be key and result[0][1] would be the value.

This regex code will match exactly what you need:

string ='"inlineParams":"json","title":"","lNameP":"MYNAME","key":"degree_result_person"},"firstName":"MYFIRSTNAME"'
import re
pattern = re.compile('\"lNameP"\:"(.*?)"')
match = pattern.search(string).group(1)
print (match)

Output:

MYNAME

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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