नीचे दिए गए कोड नमूने में मैं स्क्रैपी के अंदर एक्सपैथ का उपयोग करके, पहले सूची 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>
0
Veign
3 पद 2018, 22:13
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)
1
vezunchik
3 पद 2018, 19:30