Python Scrapy Get HTML <script> tag

蓝咒 提交于 2019-12-20 07:46:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!