【问题标题】:How to remove u'' from python script result?如何从 python 脚本结果中删除 u''?
【发布时间】:2010-07-25 14:55:01
【问题描述】:

我正在尝试使用 python/scrapy 编写解析脚本。如何从结果文件中的字符串中删除 [] 和 u'?

现在我有这样的文字:

from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from scrapy.utils.markup import remove_tags
from googleparser.items import GoogleparserItem
import sys

class GoogleparserSpider(BaseSpider):
    name = "google.com"
    allowed_domains = ["google.com"]
    start_urls = [
        "http://www.google.com/search?q=this+is+first+test&num=20&hl=uk&start=0",
    "http://www.google.com/search?q=this+is+second+test&num=20&hl=uk&start=0"
    ]

    def parse(self, response):
       print "===START======================================================="
       hxs = HtmlXPathSelector(response)
       qqq = hxs.select('/html/head/title/text()').extract()
       print qqq
       print "---DATA--------------------------------------------------------"

       sites = hxs.select('/html/body/div[5]/div[3]/div/div/div/ol/li/h3')
       i = 1
       items = []
       for site in sites:
           try:
           item = GoogleparserItem()
           title1 = site.select('a').extract()
           title2=str(title1)
           title=remove_tags(title2)
           link=site.select('a/@href').extract()
               item['num'] = i  
           item['title'] = title
               item['link'] = link
               i= i+1
               items.append(item)
           except: 
               print 'EXCEPTION'
       return items
       print "===END========================================================="

SPIDER = GoogleparserSpider()

运行后我得到了这样的结果

python scrapy-ctl.py crawl google.com

2010-07-25 17:44:44+0300 [-] Log opened.
2010-07-25 17:44:44+0300 [googleparser] DEBUG: Enabled extensions: CoreStats, CloseSpider, WebService, TelnetConsole, MemoryUsage
2010-07-25 17:44:44+0300 [googleparser] DEBUG: Enabled scheduler middlewares: DuplicatesFilterMiddleware
2010-07-25 17:44:44+0300 [googleparser] DEBUG: Enabled downloader middlewares: HttpAuthMiddleware, DownloaderStats, UserAgentMiddleware, RedirectMiddleware, DefaultHeadersMiddleware, CookiesMiddleware, HttpCompressionMiddleware, RetryMiddleware
2010-07-25 17:44:44+0300 [googleparser] DEBUG: Enabled spider middlewares: UrlLengthMiddleware, HttpErrorMiddleware, RefererMiddleware, OffsiteMiddleware, DepthMiddleware
2010-07-25 17:44:44+0300 [googleparser] DEBUG: Enabled item pipelines: CsvWriterPipeline
2010-07-25 17:44:44+0300 [-] scrapy.webservice.WebService starting on 6080
2010-07-25 17:44:44+0300 [-] scrapy.telnet.TelnetConsole starting on 6023
2010-07-25 17:44:44+0300 [google.com] INFO: Spider opened
2010-07-25 17:44:45+0300 [google.com] DEBUG: Crawled (200) <GET http://www.google.com/search?q=this+is+first+test&num=20&hl=uk&start=0> (referer: None)
===START=======================================================
[u'this is first test - \u041f\u043e\u0448\u0443\u043a Google']
---DATA--------------------------------------------------------
2010-07-25 17:52:42+0300 [google.com] DEBUG: Scraped GoogleparserItem(num=1, link=[u'http://www.amazon.com/First-Protector-Small-Tamora-Pierce/dp/0679889175'], title=u"[u'Amazon.com: First Test (Protector of the Small) (9780679889175 ...']") in <http://www.google.com/search?q=this+is+first+test&num=100&hl=uk&start=0>

文件中的这段文字:

1,[u'Amazon.com: First Test (Protector of the Small) (9780679889175 ...'],[u'http://www.amazon.com/First-Protector-Small-Tamora-Pierce/dp/0679889175']

【问题讨论】:

  • 哦,对不起,非常感谢。 ))
  • 对于其他想要删除你的人,我只是想让你知道,通常没有充分的理由来删除它。它只是将字符串标记为 unicode,但在处理它时实际上并不作为字符串的一部分出现。例如,您可以打印列表中的任何字符串,并且您可以看到 'u' 没有出现。

标签: python utf-8 web-crawler scrapy


【解决方案1】:

更漂亮 - print qqq.pop()

【讨论】:

    【解决方案2】:

    print qqq 替换为print qqq[0]。你得到这个结果是因为qqq 是一个列表。

    您的文本文件也有同样的问题。您有一个列表,其中包含您正在编写的一个元素,而不是列表中的元素。

    【讨论】:

    • 只是想知道为什么我被否决了。据我所知,我确实回答正确。
    • ??我没有贬低你。但是我尝试了您的变体,但它对我不起作用。也许我做错了什么。无论如何,我非常感谢您的帮助。
    【解决方案3】:

    看起来extract 的结果是list。试试:

    print ', '.join(qqq)
    

    【讨论】:

      【解决方案4】:

      代码前面的 u,纯粹表示它是一个 unicode 字符串。请参阅此处的参考。 http://docs.python.org/tutorial/introduction.html#unicode-strings。解决方法是使用 str() 方法将您的内容转换为字符串。

      【讨论】:

        猜你喜欢
        • 2015-02-04
        • 1970-01-01
        • 1970-01-01
        • 2020-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-04
        • 1970-01-01
        相关资源
        最近更新 更多