python - with_contains 和 without_contains 的输出是什么

标签 python scrapy selector

with_contains 和 without_contains 的输出是什么,请解释 contains,当我不使用 contains 时,会生成错误

exceptions.ValueError: Invalid XPath: //div[@class, "product-name"]"

#<div class="product-name">Adcor Defense BEAR 223 16 OPT RDY</div>

with_contains = hxs.select('//div[contains(@class, "product-name")]/text()').extract()
without_contains = hxs.select('//div[@class, "product-name"]/text()').extract()

最佳答案

你的问题的答案非常简单 - 包含是字符串函数,用于检查值中的子字符串。例如:

..
<div class="product-name other_class">Example</div>

要从此元素获取数据:

data = hxs.select('//div[contains(@class, "product-name")]/text()').extract()

如果您不使用 contains ,则意味着该类与提供的值完全相等,并且您应该使用语法 @class="value ,在您的情况下:

exactly = hxs.select('//div[@class="product-name"]/text()').extract()

关于python - with_contains 和 without_contains 的输出是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34494335/

相关文章:

silverlight - 为什么选择器类有一个内部构造函数?

objective-c - 使用选择器评估对象链

javascript - jQuery 选择器问题 : clicking one element will modify the immediate next element

python - Django 时区设置的正确方法是什么

python - 在 Docker 中运行的 Flask 应用程序找不到模板

python - Scrapy 爬虫,去除字符串中的逗号

python - 如何从 Flipkart 评论页面获取正确的选择器以进行网页抓取?

python - 多维数组中的索引重复项

python - 如何将参数传递给 celery 的任务?

javascript - 使用 Scrapy 抓取无限滚动的网站