如何在Python脚本结果中去除u''?

3

我将使用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']

哦,对不起,非常感谢。)) - Oleksii
对于其他想要删除'u'的人,我想告诉你通常没有很好的理由去删除它。它只是标记一个字符串为Unicode,但在处理字符串时实际上并不作为字符串的一部分出现。例如,您可以打印列表中的任何字符串,并且您会发现'u'并不会出现。 - THIS USER NEEDS HELP
4个回答

7
更加美观 - print qqq.pop()

6

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

对于你的文本文件,也存在同样的问题。你正在写入一个列表中的一个元素,而不是列表中的元素。


1
只是想知道为什么我被踩了。据我所知,我的回答是正确的。 - Daniel Egeberg
其实我并没有给你点踩,只是我试了一下你的方法,但对我来说没有起作用。可能是我做错了什么地方。不管怎样,非常感谢你的帮助。 - Oleksii

1

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

print ', '.join(qqq)

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接