Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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

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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/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
Python 3.x Python scrapy页面无法使用xpath_Python 3.x_Xpath_Web Scraping_Scrapy - Fatal编程技术网

Python 3.x Python scrapy页面无法使用xpath

Python 3.x Python scrapy页面无法使用xpath,python-3.x,xpath,web-scraping,scrapy,Python 3.x,Xpath,Web Scraping,Scrapy,我正在使用Python3.5删除一个html字符串,以提取该html字符串中的名称。我的代码如下: from scrapy.selector import Selector html_string = '<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:o

我正在使用Python3.5删除一个html字符串,以提取该html字符串中的名称。我的代码如下:

from scrapy.selector import Selector

html_string = '<html
    xmlns:v="urn:schemas-microsoft-com:vml"
    xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:w="urn:schemas-microsoft-com:office:word"
    xmlns="http://www.w3.org/TR/REC-html40">
    <head>
        <link href="https://www.rentlinx.com/Templates/MainStyle.css" type="text/css" rel="stylesheet" ></link>
        <style>
            <!-- /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {margin:0in; margin-bottom:.0001pt; font-size:12.0pt;} -->
        </style>
    </head>
    <body>
        <table width="100%" cellpadding="12">
            <tr>
                <td width="100%" style="background: #e8f2f5;">
                    <img src="https://www.rentlinx.com/images/page-logo-v15.png" alt="new lead" style="padding-top: 2px; padding-bottom: 2px;" />
                </td>
            </tr>
        </table>
        <br />
        <table width="100%" cellpadding="12">
            <tr>
                <td>
                    <p style="font-family: Tahoma, sans-serif; font-size: 16px; line-height: 20px; color: #666; font-weight: bold;">You have a new lead!</p>
                    <p style="font-family: Tahoma, sans-serif; font-size: 16px; line-height: 20px; color: #666;"> This 
                        <strong>basic (free)</strong> lead was generated for your property courtesy of RentLinx. 
                    </p>
                    <br />
                    <table cellpadding="7" style="width: 200px;">
                        <tr>
                            <td style="font-family: Tahoma, sans-serif; background: #009dc6; color: white; font-size: 20px; display: inline-block;"> Lead Details </td>
                        </tr>
                    </table>
                    <table cellpadding="0" cellspacing="0" border="0">
                        <tr>
                            <td style="border-right: 1px solid #CCC; width: 12px;"></td>
                            <td style="padding: 10px;">
                                <span style="font-family: Tahoma, sans-serif; color: #00aedb; font-size: 12px; font-weight: bold; text-transform: uppercase; line-height: 15px;">From:</span>
                                <br />
                                <span style="font-family: Tahoma, sans-serif; font-size: 16px; line-height: 20px; color: #666;">Foo bar</span>
                            </td>
                        </tr>
                        <tr>
                            <td style="border-right: 1px solid #CCC;"></td>
                            <td style="padding: 10px;">
                                <span style="font-family: Tahoma, sans-serif; color: #00aedb; font-size: 12px; font-weight: bold; text-transform: uppercase; line-height: 15px;">Date:</span>
                                <br />
                                <span style="font-family: Tahoma, sans-serif; font-size: 16px; line-height: 20px; color: #666;">5/21/2016 3:24:10 AM</span>
                            </td>
                        </tr>
                        <tr>
                            <td style="border-right: 1px solid #CCC;"></td>
                            <td style="padding: 10px;">
                                <span style="font-family: Tahoma, sans-serif; color: #00aedb; font-size: 12px; font-weight: bold; text-transform: uppercase; line-height: 15px;">Regarding:</span>
                                <br />
                                <span style="font-family: Tahoma, sans-serif; font-size: 16px; line-height: 20px; color: #666;">My street and your street</span>
                            </td>
                        </tr>
                        <tr>
                            <td style="border-right: 1px solid #CCC;"></td>
                            <td style="padding: 10px;">
                                <span style="font-family: Tahoma, sans-serif; color: #00aedb; font-size: 12px; font-weight: bold; text-transform: uppercase; line-height: 15px;">Contact Information:</span>
                                <br />
                                <span class="value" style="line-height: 28px; padding-top: 5px;">
                                    <a href="tel:1112223333" title="Call" style="color: #007998; font-family: Tahoma, sans-serif; font-size: 16px; line-height: 20px; text-decoration: none;">(111) 222-3333</a>
                                    <br />
                                    <a href="mailto:foobar@gmail.com" title="Email" style="color: #007998; font-family: Tahoma, sans-serif; font-size: 16px; line-height: 20px; text-decoration: none;">foobar@gmail.com</a>
                                </span>
                            </td>
                        </tr>
                        <tr>
                            <td style="border-right: 1px solid #CCC;"></td>
                            <td style="padding: 10px;">
                                <span style="font-family: Tahoma, sans-serif; color: #00aedb; font-size: 12px; font-weight: bold; text-transform: uppercase; line-height: 15px;">Comments:</span>
                                <br />
                                <span style="font-family: Tahoma, sans-serif; font-size: 16px; line-height: 20px; color: #666;"> Hi, I like your apartment. Thanks </span>
                            </td>
                        </tr>
                        <tr>
                            <td style="border-right: 1px solid #CCC;"></td>
                            <td style="padding: 10px;">
                                <span style="font-family: Tahoma, sans-serif; color: #00aedb; font-size: 12px; font-weight: bold; text-transform: uppercase; line-height: 15px;">Lead From:</span>
                                <br />
                                <span style="font-family: Tahoma, sans-serif; font-size: 16px; line-height: 20px; color: #666;">
                                    <a href="https://www.marsplanet.com/13933360/" title="Lead from Mars Planet" style="font-family: Tahoma, sans-serif; font-size: 16px; line-height: 20px; color: #666;">MarsPlanet</a>
                                </span>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
        <table width="100%" cellpadding="12">
            <tr>
                <td>
                    <p style="font-family: tahoma, sans-serif; font-size: 16px; line-height: 20px; color: #666;"> Thanks,
                        <br /> The RentLinx Team 
                    </p>
                    <p style="background-color: #3f3d5d; color: White; padding: 8px; "> Want more leads like this? Upgrade your property to RentLinx 
                        <strong>
                            <em>Plus!</em>
                        </strong> today! Just 
                        <a href="https://www.rentlinx.com" style="color: #fff;">login to RentLinx</a>, then click "Go Plus!" 
                    </p>
                    <p>
                        <a href="http://www.facebook.com/rentlinx">
                            <img src="https://www.rentlinx.com/images/facebook/FB-f-Logo__blue_29.png" width="29" height="29" style="margin: 8px; border: 0;" align="absmiddle" />
                        </a>Like RentLinx? Please like us on facebook! 
                        <a href="http://www.facebook.com/rentlinx">www.facebook.com/rentlinx</a>
                    </p>
                </td>
            </tr>
        </table>
        <img src="http://delivery.rentlinx.com/" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;"/>
    </body>
</html>'

s = Selector(text=html_string)
name = s.xpath('/html/body/table[2]/tbody/tr/td/table[2]/tbody/tr[1]/td[2]/span[2]/text()').extract()[0]

print(name)
从scrapy.selector导入选择器
html_字符串=

您有了新的潜在客户

这个 基本(免费)由RentLinx为您的酒店生成潜在客户。


潜在客户详细信息 发件人:
富吧 日期:
2016年5月21日凌晨3:24:10 关于:
我的街道和你的街道 联系方式:

评论:
嗨,我喜欢你的公寓。谢谢 牵头人:
谢谢,
RentLinx团队

想要更多这样的潜在客户吗?将您的物业升级到RentLinx 加上! 今天!只是 ,然后单击“Go Plus!”

像RentLinx?请在facebook上喜欢我们!

' s=选择器(文本=html\U字符串) name=s.xpath('/html/body/table[2]/tbody/tr/td/table[2]/tbody/tr[1]/td[2]/span[2]/text()')。extract()[0] 印刷品(名称)
它应该打印Foo-bar,因为这是我想要得到的。但它给出了空字符串。你知道我做错了什么吗?另外,我在html代码中没有看到一个名为
tbody
的标记,但你还是在调用它。我知道,在现代浏览器中,当您在inspect选项中看到它时,可能会产生误导,但它根本不在代码中

实际上,从2016年5月11日起,您应该使用选择器

试试这个

name = response.xpath('/html/body/table[2]/tr/td/table[2]/tr[1]/td[2]/span[2]/text()').extract_first()

print(name)
另外,我在html代码中没有看到一个名为
tbody
的标记,但您还是在调用它。我知道,在现代浏览器中,当您在inspect选项中看到它时,可能会产生误导,但它根本不在代码中

实际上,从2016年5月11日起,您应该使用选择器

试试这个

name = response.xpath('/html/body/table[2]/tr/td/table[2]/tr[1]/td[2]/span[2]/text()').extract_first()

print(name)

谢谢拉斐尔。是的,
tobdoy
出现在inspector中,但没有tin html。它起作用了。即使是
Selector
也可以,如果我按照您的建议使用xpath
name=Selector.xpath('/html/body/table[2]/tr/td/table[2]/tr[1]/td[2]/span[2]/text())。extract_first()
为什么建议使用
response
而不是
Selector
?应该说,在爬网开始时,Selector就被弃用了。如果你发现我的答案有帮助,请考虑接受它作为正确答案=)编辑->使我意识到这实际上是错误的,选择器应该是走的路!谢谢拉斐尔。是的,
tobdoy
出现在inspector中,但没有tin html。它起作用了。即使是
Selector
也可以,如果我按照您的建议使用xpath
name=Selector.xpath('/html/body/table[2]/tr/td/table[2]/tr[1]/td[2]/span[2]/text())。extract_first()
为什么建议使用
response
而不是
Selector
?应该说,在爬网开始时,Selector就被弃用了。如果你发现我的答案有帮助,请考虑接受它作为正确答案=)编辑->使我意识到这实际上是错误的,选择器应该是走的路!