我正在使用“ scrapy”来刮几篇文章,例如:https://fivethirtyeight.com/features/championships-arent-won-on-paper-but-what-if-they-were/
我在蜘蛛中使用以下代码:
def calcula_masa_atomica(molecula):
return sum(masas.get(atom,0) * int(nb or 1) for _,atom,nb in re.findall(r'(([A-Z])([0-9]*))',molecula))
...有效。但是我想使这个CSS选择器更加复杂。 现在,我正在提取每个文本段落。但是看这篇文章,里面有表格和可视化内容,其中也包括文本。 HTML结构如下所示:
def parse_article(self,response):
il = ItemLoader(item=Scrapping538Item(),response=response)
il.add_css('article_text','.entry-content *::text')
上面的代码被剪断了,我得到了类似的东西:
我想要的文字
我要的文字
我要的文字
TITLE文字 SUB-TITLE-文本 表格数据 我要的文字
我想要的文字
我的问题:
- 如何以某种方式修改
<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>
函数 是否需要除表格中的文字以外的所有文字? - 使用功能
add_css()
会更容易吗? - 通常,最佳做法是什么? (提取文字 在一定条件下)
非常感谢您反馈