使用正則表達式來做到這一點
首先抓取該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)