2017-04-05 97 views
-3

Xpath无法在Google Chrome中使用。Xpath无法正常工作Google Chrome for Xml Document Scrapy Python

$ X( '// URL')

回报= []#空

$ X('// DIV [@ ID = “WebKit的XML的观察者源-XML”] “)

收益= [] #Empty

<div xmlns="http://www.w3.org/1999/xhtml" id="webkit-xml-viewer-source-xml"> 
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> 
    <url> 
     <loc>https://www.sample.co.uk/</loc> 
     <changefreq>daily</changefreq> 
    </url> 

    <url> 
     <loc>https://www.sample1.co.uk/motherboardbundles/</loc> 
     <changefreq>daily</changefreq> 
    </url> 

    <url> 
     <loc>https://www.sample2.co.uk/barebonebundles/</loc> 
     <changefreq>daily</changefreq> 
    </url> 
</urlset></div> 

请告诉我什么是错我的XPath。 任何帮助将不胜感激。

感谢 Syyed

请参阅实际的HTML的图像下方。 enter image description here

对于xpath请参见下文。

enter image description here 你可以看到,当我在开发人员工具(谷歌浏览器)中使用Xpath时,我得到NULL(空)。

+0

你必须告诉我们更多关于如何在文档上运行XPath(即显示一些代码) –

+0

''// url''意味着没有命名空间的url,但是您似乎使用了命名空间。您也需要在XPath表达式中指定它。 – choroba

+0

您可以阅读关于Scrapy选择器和XML命名空间[这里](https://docs.scrapy.org/en/latest/topics/selectors.html#removing-namespaces)和[here](https://parsel.readthedocs .IO/EN /最新/ usage.html中#特设的命名空间引用)。 –

回答

0

谢谢@保罗在请求中使用头我得到了我的结果。

导入请求 从parsel进口选择 文本= requests.get( 'https://www.sample.co.uk/sitemap.xml',标题= { '的User-Agent': '我的用户代理'})。文字