Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xpath 如何在JavaScript源代码中选择元素?_Xpath_Scrapy - Fatal编程技术网

Xpath 如何在JavaScript源代码中选择元素?

Xpath 如何在JavaScript源代码中选择元素?,xpath,scrapy,Xpath,Scrapy,我需要在下面的JavaScript源代码中获取“html”键的值,该源代码由xpath(“.//script[34]”提取并嵌入到html源页面中 <script> FM.view({ "ns": "pl.content.homeFeed.index", "domid": "Pl_Official_MyProfileFeed__24", "css": ["style/css/module/l

我需要在下面的JavaScript源代码中获取“html”键的值,该源代码由xpath(“.//script[34]”提取并嵌入到html源页面中

   <script>
        FM.view({
            "ns": "pl.content.homeFeed.index",
            "domid": "Pl_Official_MyProfileFeed__24",
            "css": ["style/css/module/list/comb_WB_feed_profile.css?version=73267f08bd52356e"],
            "js": "page/js/pl/content/homeFeed/index.js?version=dad90e594db2c334",
            "html": "                <div class=\"WB_feed WB_feed_v3\" pageNum=\"\" node-type='feed_list' module-type=\"feed\">\r\n...."
        })
    </script>

FM.view({
“ns”:“pl.content.homeFeed.index”,
“domid”:“Pl_官方文件\u MyProfileFeed \u 24”,
“css”:[“style/css/module/list/comb_WB_feed_profile.css?version=73267f08bd52356e”],
“js”:“page/js/pl/content/homeFeed/index.js?version=dad90e594db2c334”,
“html”:“\r\n…”
})
我特别不知道如何处理文本“FM.view”

我将使用
.re()
从脚本中提取
html
键值:

>>> response.xpath("//script[contains(., 'Pl_Official_MyProfileFeed__24')]/text()").re(r'"html": "(.*?)"\n')
[0].strip()
u'<div class=\\"WB_feed WB_feed_v3\\" pageNum=\\"\\" node-type=\'feed_list\' module-type=\\"feed\\">\\r\\n..'

请注意正则表达式中的
(?ms)
部分-这是我们在本例中设置模式工作所需的标志-多行和dotall的方式。

这里是使用包的正则表达式+json的替代方法

第一步是从HTML获取
中的JavaScript语句。你可能已经迈出了这一步。在这里,我将根据您的输入HTML构建一个粗略的选择器。在您的情况下,您可能正在使用回调中的
响应

>>> import scrapy
>>> import js2xml
>>> t = r'''   <script>
...         FM.view({
...             "ns": "pl.content.homeFeed.index",
...             "domid": "Pl_Official_MyProfileFeed__24",
...             "css": ["style/css/module/list/comb_WB_feed_profile.css?version=73267f08bd52356e"],
...             "js": "page/js/pl/content/homeFeed/index.js?version=dad90e594db2c334",
...             "html": "                <div class=\"WB_feed WB_feed_v3\" pageNum=\"\" node-type='feed_list' module-type=\"feed\">\r\n...."
...         })
...     </script>'''
>>> selector = scrapy.Selector(text=t, type='html')
第三是从树中选择所需的对象。 这里,它是
FM.view()
调用的第一个参数。在lxml树上调用
.xpath()
,即使您选择了1个节点,也会得到一个列表(xpath返回节点集)

最后,您只需使用该命令中的“html”键:

>>> jsdata = js2xml.jsonlike.make_dict(args[0])
>>> jsdata['html']
'                <div class="WB_feed WB_feed_v3" pageNum="" node-type=\'feed_list\' module-type="feed">\r\n....'
>>> 
jsdata=js2xml.jsonlike.make_dict(args[0]) >>>jsdata['html'] “\r\n…” >>>
非常感谢,但请告诉我包含函数的第一个参数point“.”的意思是什么?@lerneradams是的,这是元素文本的快捷方式。使用它来获取包含
Pl\u Official\u MyProfileFeed\uu 24
文本的脚本。希望有帮助。
>>> js = selector.xpath('//script/text()').extract_first()
>>> jstree = js2xml.parse(js)
>>> jstree
<Element program at 0x7ff19ec94ea8>
>>> type(jstree)
<type 'lxml.etree._Element'>

>>> print(js2xml.pretty_print(jstree))
<program>
  <functioncall>
    <function>
      <dotaccessor>
        <object>
          <identifier name="FM"/>
        </object>
        <property>
          <identifier name="view"/>
        </property>
      </dotaccessor>
    </function>
    <arguments>
      <object>
        <property name="ns">
          <string>pl.content.homeFeed.index</string>
        </property>
        <property name="domid">
          <string>Pl_Official_MyProfileFeed__24</string>
        </property>
        <property name="css">
          <array>
            <string>style/css/module/list/comb_WB_feed_profile.css?version=73267f08bd52356e</string>
          </array>
        </property>
        <property name="js">
          <string>page/js/pl/content/homeFeed/index.js?version=dad90e594db2c334</string>
        </property>
        <property name="html">
          <string>                &lt;div class="WB_feed WB_feed_v3" pageNum="" node-type='feed_list' module-type="feed"&gt;&#13;
....</string>
        </property>
      </object>
    </arguments>
  </functioncall>
</program>
# select the function call for "FM.view"
# and get first argument
>>> jstree.xpath('''
        //functioncall[
            function[.//identifier/@name="FM"]
                    [.//identifier/@name="view"]]
            /arguments
                /*[1]''')
[<Element object at 0x7ff19ec94ef0>]
>>> args = jstree.xpath('//functioncall[function[.//identifier/@name="FM"][.//identifier/@name="view"]]/arguments/*[1]')
# use js2xml.jsonlike.make_dict() on that argument
>>> js2xml.jsonlike.make_dict(args[0])
{'ns': 'pl.content.homeFeed.index', 'html': '                <div class="WB_feed WB_feed_v3" pageNum="" node-type=\'feed_list\' module-type="feed">\r\n....', 'css': ['style/css/module/list/comb_WB_feed_profile.css?version=73267f08bd52356e'], 'domid': 'Pl_Official_MyProfileFeed__24', 'js': 'page/js/pl/content/homeFeed/index.js?version=dad90e594db2c334'}
>>> from pprint import pprint
>>> pprint(js2xml.jsonlike.make_dict(args[0]))
{'css': ['style/css/module/list/comb_WB_feed_profile.css?version=73267f08bd52356e'],
 'domid': 'Pl_Official_MyProfileFeed__24',
 'html': '                <div class="WB_feed WB_feed_v3" pageNum="" node-type=\'feed_list\' module-type="feed">\r\n....',
 'js': 'page/js/pl/content/homeFeed/index.js?version=dad90e594db2c334',
 'ns': 'pl.content.homeFeed.index'}
>>> 
>>> jsdata = js2xml.jsonlike.make_dict(args[0])
>>> jsdata['html']
'                <div class="WB_feed WB_feed_v3" pageNum="" node-type=\'feed_list\' module-type="feed">\r\n....'
>>>