如何从python抓取的数据中删除“ \ n”?

灵魂姐姐

此链接中有一个有关抓取数据的文本文件:https : //drive.google.com/file/d/1iu_rJUb-3EROWbctugdlUp9w0JNeiTvY/view?usp=sharing

我想使用Scrapy从抓取的数据中删除\n\t字符。

def parse_item(self, response):
    item = TutorialItem()
    sel = Selector(response)
    item['url'] = response.url[0].strip()
    item['title'] = response.meta['link_text']

    # extracting basic body

    item['body'] = w3lib.html.remove_tags(w3lib.html.remove_tags_with_content(sel.xpath('//body').extract()[0].replace("\r\n", " "),which_ones=('script',)))


    with open('abc.txt', 'a') as f:
      f.write('body: {0}\n'.format(item['body']))

    return item

在上面的代码中,我删除了一些\r\n字符,但不是全部。

item['body'] = w3lib.html.remove_tags(w3lib.html.remove_tags_with_content(sel.xpath('//body').extract()[0].replace("\r\n", " "), which_ones=('script',)))
拉克什

尝试单独更换它们:

例如:

item['body'] = w3lib.html.remove_tags(w3lib.html.remove_tags_with_content(sel.xpath('//body').extract()[0].replace("\n", " ").replace("\r", ""),which_ones=('script',)))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章