首页 > 解决方案 > 我怎样才能得到

元素

问题描述

我正在尝试使用 scrapy shell 仅从数据库中抓取联系信息:

<div class="info-section">
                    <h3>State(s) Served:</h3>
                    <p>Nationwide (US)</p>  </div>
<div class="info-section">
                    <h3>Year Founded:</h3>
                    <p>1985</p>  </div>
                
<div class="info-section">
                    <h3>Description:</h3>
                    <p>Corporate tax accounting/consulting. Specialties:  280E Compliance/Planning, Research & Development Tax Credits, Cost Segregation, IRS Representation, Certified Financial Auditing.</p> </div>
                                    <div class="info-section">
                        <h3>Contact:</h3>
                        <p><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="93f1e1eaf2fdd3f0e3f2fef7bdf0fcfe">[email&#160;protected]</a> | 847-382-1166 X28</p>
                    </div>

我选择了使用信息部分sel = response.css('.info-section'),然后我可以遍历p元素,但是我如何只选择<h3>具有联系信息的标签然后获取<p>文本?

标签: pythonxpathscrapy

解决方案


如果您需要通过电子邮件获取该文本<p><a>您可以尝试以下操作:

>>> txt = """<div class="info-section">
...                     <h3>State(s) Served:</h3>
...                     <p>Nationwide (US)</p>  </div>
... <div class="info-section">
...                     <h3>Year Founded:</h3>
...                     <p>1985</p>  </div>
... 
... <div class="info-section">
...                     <h3>Description:</h3>
...                     <p>Corporate tax accounting/consulting. Specialties:  280E Compliance/Planning, Research & Development Tax Credits, Cost Segregation, IRS Representation, Certified Financial Auditing.</p> </div>
...                                     <div class="info-section">
...                         <h3>Contact:</h3>
...                         <p><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="93f1e1eaf2fdd3f0e3f2fef7bdf0fcfe">[email&#160;protected]</a> | 847-382-1166 X28</p>
...                     </div>"""
>>> from scrapy import Selector
>>> sel = Selector(text=txt)
>>> sel.xpath('//h3[contains(text(), "Contact")]/following-sibling::p/a/following-sibling::text()').get()
u' | 847-382-1166 X28'

甚至更短,正如@Jack Fleeting 所说:

>>> sel.xpath('//h3[contains(text(), "Contact")]/following-sibling::p/text()').get()
u' | 847-382-1166 X28'

推荐阅读