【问题标题】:Formatting text output with Scrapy in Python在 Python 中使用 Scrapy 格式化文本输出
【发布时间】:2011-12-18 04:45:44
【问题描述】:

我正在尝试使用 Scrapy 蜘蛛抓取页面,然后将这些页面以可读形式保存到 .txt 文件中。我用来执行此操作的代码是:

def parse_item(self, response):
        self.log('Hi, this is an item page! %s' % response.url) 

        hxs = HtmlXPathSelector(response)

        title = hxs.select('/html/head/title/text()').extract() 
        content = hxs.select('//*[@id="content"]').extract() 

        texts = "%s\n\n%s" % (title, content) 

        soup = BeautifulSoup(''.join(texts)) 

        strip = ''.join(BeautifulSoup(pretty).findAll(text=True)) 

        filename = ("/Users/username/path/output/Hansard-" + '%s'".txt") % (title) 
        filly = open(filename, "w")
        filly.write(strip) 

我在这里结合了 BeautifulSoup,因为正文包含很多我不希望在最终产品中出现的 HTML(主要是链接),所以我使用 BS 去除了 HTML,只留下了兴趣。

这给了我看起来像

的输出
[u"School, Chandler's Ford (Hansard, 30 November 1961)"]

[u'

 \n      \n

  HC Deb 30 November 1961 vol 650 cc608-9

 \n

  608

 \n

  \n


  \n

   \n

    \xa7

   \n

    28.

   \n


     Dr. King


   \n

    \n            asked the Minister of Education what is the price at which the Hampshire education authority is acquiring the site for the erection of Oakmount Secondary School, Chandler\'s Ford; and why he refused permission to acquire this site in 1954.\n

   \n

  \n

 \n      \n

  \n


  \n

   \n

    \xa7

   \n


     Sir D. Eccles


   \n

    \n            I understand that the authority has paid \xa375,000 for this site.\n            \n

虽然我希望输出看起来像:

    School, Chandler's Ford (Hansard, 30 November 1961)

          HC Deb 30 November 1961 vol 650 cc608-9

          608

            28.

Dr. King asked the Minister of Education what is the price at which the Hampshire education authority is acquiring the site for the erection of Oakmount Secondary School, Chandler's Ford; and why he refused permission to acquire this site in 1954.

Sir D. Eccles I understand that the authority has paid £375,000 for this site.

所以我基本上是在寻找如何删除换行符\n,收紧所有内容,并将任何特殊字符转换为正常格式。

【问题讨论】:

    标签: python text web-scraping scrapy


    【解决方案1】:

    我在 cmets 中对代码的回答:

    import re
    import codecs
    
    #...
    #...
    #extract() returns list, so you need to take first element
    title = hxs.select('/html/head/title/text()').extract() [0]
    content = hxs.select('//*[@id="content"]')
    #instead of using BeautifulSoup for this task, you can use folowing
    content = content.select('string()').extract()[0]
    
    #simply delete duplicating spaces and newlines, maybe you need to adjust this expression
    cleaned_content = re.sub(ur'(\s)\s+', ur'\1', content, flags=re.MULTILINE + re.UNICODE)
    
    texts = "%s\n\n%s" % (title, cleaned_content) 
    
    #look's like typo in filename creation
    #filename ....
    
    #and my preferable way to write file with encoding
    with codecs.open(filename, 'w', encoding='utf-8') as output:
        output.write(texts)
    

    【讨论】:

    • 感谢您的评论。然而,每当我运行它时,我都会收到一个错误:cleaned_content = re.sub(ur'(\s)\s+', ur'\1', content, flags=re.MULTILINE + re.UNICODE) exceptions.TypeError: sub() got an unexpected keyword argument 'flags'。有什么想法吗?
    • @user1074057 你使用的是python strip_re = re.compile(ur'(\s)\s+', re.MULTILINE + re.UNICODE); cleaned_content = strip_re.sub(ur'\1', content)
    • 效果很好。接受并赞成。感谢您的帮助!
    • 快速提问。我注意到,当输出被写入时,它用 'äô 替换了一个 '。关于如何解决这个问题的任何想法?
    • @user1074057,也许编辑器加载它不是utf-8 编码?或者您可以尝试使用页面编码保存它(iso-8859-1 如果我在同一页面上搜索)
    猜你喜欢
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多