python – 两个关键字之间的scrapy xpath
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 两个关键字之间的scrapy xpath,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5201字,纯文字阅读大概需要8分钟。
内容图文
我试图在2个关键字之间提取一些文本信息,如下所示:
item['duties']=titles.select('.//span/text()[following-sibling::*[text()="Qualifications/Duties" and preceding-sibling::*text()="Entity Information"]').extract()
蜘蛛:
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.http import request
from scrapy.selector import HtmlXPathSelector
from health.items import HealthItem
class healthspider(CrawlSpider):
name="health"
allowed_domains=['mysite']
start_urls=['myurl']
rules=(
Rule(SgmlLinkExtractor(allow=("search/",)), callback="parse_health", follow=True),
Rule(SgmlLinkExtractor(allow=("url",)),callback="parse_job",follow=True),
)
def parse_job(self, response):
hxs=HtmlXPathSelector(response)
titles=hxs.select('//*[@itemprop="description"]')
items = []
for titles in titles:
item=HealthItem()
item['duties']=titles.select('.//span[following-sibling::*[text()="Qualifications/Duties" and preceding-sibling::*text()="Entity Information"]/text()').extract()
item['entity_info']=titles.select('.//span/text()[45]').extract() items.append(item)
print items
return items
但我得到一个错误说:
raise ValueError("Invalid XPath: %s" % query)
exceptions.ValueError: Invalid XPath: .//span/text()[following-sibling::*[text()="Qualifications/Duties" and preceding-sibling::*text()="Entity Information"]
有没有办法在我的蜘蛛中定义这样的xpath?
解决方法:
几个选项:
使用node()测试选择(文本节点和元素节点)
In [1]: sel.xpath(""".//node()[preceding-sibling::*="Qualifications/Duties"]
[following-sibling::*="Entity Information"]""").extract()
Out[1]:
[u'<br>',
u'Texas Health Presbyterian Allen is currently in search of a Registered Nurse to help meet the growing needs of our Day Surgery Department to work PRN in Day Surgery and also float to PACU.',
u'<br>',
u'<br>',
u'<b>Basic Qualifications:</b>',
u'<br>',
u'<br>',
u'*Graduate of an accredited school of nursing',
u'<br>',
u'*Valid RN license in the state of Texas',
u'<br>',
u'*BLS',
u'<br>',
u'*ACLS',
u'<br>',
u'*PALS within 6 months of hire',
u'<br>',
u'*Minimum of 1 - 3 years experience as RN in Day Surgery, PACU, Outpatient Surgery, or ICU',
u'<br>',
u'*Strong organizational skills and ability to function in a fast paced work environment',
u'<br>',
u'*Ability to accept responsibility and show initiative to work without direct supervision',
u'<br>',
u'*A high degree of confidentiality, positive interpersonal skills and ability to function in a fast-paced environment',
u'<br>',
u'<br>',
u'<b>Preferred Qualifications:</b>',
u'<br>',
u'<br>',
u'*Three years RN experience in Outpatient Surgery along with some ICU experience.',
u'<br>',
u'*PALS',
u'<br>',
u'*PACU , Endoscopy or Ambulatory setting',
u'<br>',
u'*IV Conscious Sedation',
u'<br>',
u'<br>',
u'<b>Hours/Schedule:</b>',
u'<br>',
u'<br>',
u'*Variable',
u'<br>',
u'<br>',
u'J2WPeriop',
u'<br>',
u'<br>']
仅选择文本节点:(您“松开”粗线)
In [2]: sel.xpath(""".//text()[preceding-sibling::*="Qualifications/Duties"]
[following-sibling::*="Entity Information"]""").extract()
Out[2]:
[u'Texas Health Presbyterian Allen is currently in search of a Registered Nurse to help meet the growing needs of our Day Surgery Department to work PRN in Day Surgery and also float to PACU.',
u'*Graduate of an accredited school of nursing',
u'*Valid RN license in the state of Texas',
u'*BLS',
u'*ACLS',
u'*PALS within 6 months of hire',
u'*Minimum of 1 - 3 years experience as RN in Day Surgery, PACU, Outpatient Surgery, or ICU',
u'*Strong organizational skills and ability to function in a fast paced work environment',
u'*Ability to accept responsibility and show initiative to work without direct supervision',
u'*A high degree of confidentiality, positive interpersonal skills and ability to function in a fast-paced environment',
u'*Three years RN experience in Outpatient Surgery along with some ICU experience.',
u'*PALS',
u'*PACU , Endoscopy or Ambulatory setting',
u'*IV Conscious Sedation',
u'*Variable',
u'J2WPeriop']
从粗体行中选择希望文本节点的节的兄弟节点::
In [3]: sel.xpath(""".//*[preceding-sibling::*="Qualifications/Duties"]
[following-sibling::*="Entity Information"]/text()
|
.//text()[preceding-sibling::*="Qualifications/Duties"]
[following-sibling::*="Entity Information"]""").extract()
Out[3]:
[u'Texas Health Presbyterian Allen is currently in search of a Registered Nurse to help meet the growing needs of our Day Surgery Department to work PRN in Day Surgery and also float to PACU.',
u'Basic Qualifications:',
u'*Graduate of an accredited school of nursing',
u'*Valid RN license in the state of Texas',
u'*BLS',
u'*ACLS',
u'*PALS within 6 months of hire',
u'*Minimum of 1 - 3 years experience as RN in Day Surgery, PACU, Outpatient Surgery, or ICU',
u'*Strong organizational skills and ability to function in a fast paced work environment',
u'*Ability to accept responsibility and show initiative to work without direct supervision',
u'*A high degree of confidentiality, positive interpersonal skills and ability to function in a fast-paced environment',
u'Preferred Qualifications:',
u'*Three years RN experience in Outpatient Surgery along with some ICU experience.',
u'*PALS',
u'*PACU , Endoscopy or Ambulatory setting',
u'*IV Conscious Sedation',
u'Hours/Schedule:',
u'*Variable',
u'J2WPeriop']
内容总结
以上是互联网集市为您收集整理的python – 两个关键字之间的scrapy xpath全部内容,希望文章能够帮你解决python – 两个关键字之间的scrapy xpath所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。