简体   繁体   中英

Python - How to find a word in string, then print only x letters afterwards

So with following im adding Script from a webpage to a dict:

script2 = []
script1 = soup.findAll("script")[2]
script2.append(script1)
print(script2)

The output is a wall of text, i only want to extract certain things out of it.

How do I filter after a special string and then only print x letters afterwards? With that i could extract everything I want out of it.

Note: I already tried it with json loads but it doesnt work here strangely (example of the script output: https://pastebin.com/xvzQ456P )

You should be able to do that with regex.

import re

script2 = ...

special_string = 'xxx'
x_letters_afterwards = 5

result = re.findall(special_string + "(.{" + x_letters_afterwards + "})", script2)

print(result)

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