问题
I have a project and i need the get script in html code.
<script>
(function() {
... / More Code
Level.grade = "2";
Level.level = "1";
Level.max_line = "5";
Level.cozum = 'adım 12\ndön sağ\nadım 13\ndön sol\nadım 11';
... / More Code
</script>
How i get only " adım 12\ndön sağ\nadım 13\ndön sol\nadım 11 " this code?
Thanks for Helps
回答1:
Use Regex to do that
First grab the content of that SCRIPT tag like
response.css("script").extract_first()
And then use this regex
(Level\.cozum = )(.*?)(\;)
See demo here https://regex101.com/r/YxHRmR/1
This is code
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)
来源:https://stackoverflow.com/questions/43871064/python-scrapy-get-html-script-tag