我有一个项目,我需要html代码中的get脚本。


 < script>
 (function(){
 ... /更多Code
 Level.grade =“2”;

 Level.level =“1”;

 Level.max_line =“5”;

 Level.cozum ='adım12\ndönsağ\nadım13\ndönsol\nadım11';
 ... /更多Code
 < / script>



 我如何只获得“adım12\ndönsağ\nadım13\ndönsol\nadım11”此代码?


感谢帮助

答案 0 :(得分:0)
使用Regex执行此操作
首先获取该SCRIPT标记的内容,如
response.css("script").extract_first()
然后使用此正则表达式
(Level\.cozum = )(.*?)(\;)
在此处查看演示https://regex101.com/r/YxHRmR/1
这是代码
import re
regex = r"(Level\.cozum = )(.*?)(\;)"
test_str = ("<script>\n"
" (function() {\n"
" ... / More Code\n"
" Level.grade = \"2\";\n\n"
" Level.level = \"1\";\n\n"
" Level.max_line = \"5\";\n\n"
" Level.cozum = 'adım 12\\ndön sağ\\nadım 13\\ndön sol\\nadım 11'; \n"
"... / More Code\n"
"</script>")
matches = re.findall(regex, test_str, re.MULTILINE)
print(matches)