使用Xpath获取两个都包含在同一容器中的列表

时间:2018-12-03 19:13:17

标签: xpath scrapy

在下面的代码示例中,我希望使用Scrapy中的Xpath进行提取,首先从列表1中提取,然后从列表2中提取。有些项目可能被链接了出去,而另一些只是列表中的项目。我需要的是两个字符串(或列表),一个用于列表1,一个用于列表2

	<div class="row">
		<div class="col-xs-12 no-padding-xs">
			<h3 class="text-primary gutter-xs">List 1</h3>
			<div class="well well-sm">
				<a href="/miniature.htm">Miniature</a>, <a href="/mustang.htm">Mustang</a>, <a href="/paintpony.htm">Paint Pony</a>, <a href="https://www.equinenow.com/browse-brf-pi">Pinto</a>, <a href="/pony.htm">Pony</a>, <a href="/poa.htm">POA</a>, <a href="/quarterpony.htm">Quarter Pony</a>, <a href="/shetlandpony.htm">Shetland Pony</a>, <a href="/spanishmustang.htm">Spanish Mustang</a>
			</div>
			<h3 class="text-primary gutter-xs">List 2</h3>
			<div class="well well-sm">
				<a href="/allaroundfarms.htm">All Around</a>, <a href="/drivingfarms.htm">Driving</a>, <a href="/halterfarms.htm">Halter</a>, <a href="/lessonfarms.htm">Lesson</a>, <a href="/naturalhorsemanshipfarms.htm">Natural Horsemanship</a>, <a href="/showfarms.htm">Show</a>, Trail Riding, <a href="/westernpleasurefarms.htm">Western Pleasure</a>, Western Riding, <a href="/youthfarms.htm">Youth</a>, Champion Trainer, POA Ponies for Sale, Newaygo County, Horse Boarding, Equestrian Coaching, Michigan, Riding Lessons, Horse Lea
			</div>
		</div>
	</div>

1 个答案:

答案 0 :(得分:1)

不确定我是否正确理解了您,但是您可以尝试:

from w3lib.html import remove_tags

for list_text in ['List 1', 'List 2']:
    div_data = response.xpath('//h3[text()="{}"]/following-sibling::div[1]'.format(list_text)).get()
    if not div_data:
         continue
    print [remove_tags(i).strip() for i in div_data.split(',')]

或者如果您只想输入字符串:

for list_text in ['List 1', 'List 2']:
    div_data = response.xpath('//h3[text()="{}"]/following-sibling::div[1]'.format(list_text)).get()
    if not div_data:
         continue
    print remove_tags(div_data)