【发布时间】:2020-03-06 14:51:56
【问题描述】:
我正在使用“scrapy”来抓取一些文章,例如:https://fivethirtyeight.com/features/championships-arent-won-on-paper-but-what-if-they-were/
我在我的蜘蛛中使用以下代码:
def parse_article(self, response):
il = ItemLoader(item=Scrapping538Item(), response=response)
il.add_css('article_text', '.entry-content *::text')
...这行得通。但我想让这个 CSS 选择器更复杂一点。 现在,我正在提取每个文本段落。但是看看这篇文章,里面有表格和可视化,其中也包括文本。 HTML 结构如下所示:
<div class="entry-content single-post-content">
<p>text I want</p>
<p>text I want</p>
<p>text I want</p>
<section class="viz">
<header class="viz">
<h5 class="title">TITLE-text</h5>
<p class="subtitle">SUB-TITLE-text</p>
</header>
<table class="viz full"">TABLE DATA</table>
</section>
<p>text I want</p>
<p>text I want</p>
</div>
使用上面的代码,我得到了类似的东西:
我想要的文字
我想要的文字
我想要的文字
TITLE-文本 SUB-TITLE-text 表数据 我想要的文字
我想要的文字
我的问题:
- 如何修改
add_css()函数,使其 获取除表格中的文本之外的所有文本? - 使用函数
add_xpath会更容易吗? - 一般来说,最好的做法是什么? (提取文本 条件下)
非常感谢您的反馈
【问题讨论】:
标签: python scrapy css-selectors web-crawler