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 protected]</a> | 847-382-1166 X28</p>
</div>
我选择了使用信息部分sel = response.css('.info-section')
,然后我可以遍历p
元素,但是我如何只选择<h3>
具有联系信息的标签然后获取<p>
文本?
解决方案
如果您需要通过电子邮件获取该文本<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 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'
推荐阅读
- c# - 测试视频录制未上传到测试结果 Azure Devops
- python - 当窗口改变其大小时,Tkinter 网格不会扩展
- python - 如何在迭代数字的数字时形成链表?
- c++ - 有没有办法,或者需要编写一个通用的模板字符文字
- r - 无法从 Yahoo R 下载索引数据
- java - 如何修复在vs代码中生成maven项目的失败
- css - 需要 CSS(PHP 不起作用)根据设备类型在 Wordpress TwentySeventeen 中交换标题图像
- security - 联系 id 接收器窃贼警报信号并将它们推送到我的 webapp
- python-3.x - How to plot median value on boxplot?
- react-native - React native Fire base Push notification not working in OPO,VIVO, MI when app is close