Python Scrapy Get HTML [removed] tag

2020-05-01 05:53发布

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条回答
三岁会撩人
2楼-- · 2020-05-01 06:43

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)
查看更多
登录 后发表回答