如何使用scrapy response获取其<h3>标签包含单词“Contact:”的<p>元素

2024-09-27 19:27:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图用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')选择了info部分,然后我可以迭代p元素,但是如何只选择包含联系人信息的<h3>标记,然后获得<p>文本?你知道吗


Tags: divinfo信息数据库email联系人sectionshell
1条回答
网友
1楼 · 发布于 2024-09-27 19:27:35

如果您需要通过电子邮件获取<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'

相关问题 更多 >

    热门问题