search

Home  >  Q&A  >  body text

python - 抓取下的unicode编码转urlcode

    articles=response.css("#content > article:nth-child(5) p")
    sgg="";
    for item in articles:
        sgg = sgg+item.extract()
    data=urllib.urlencode({ 'title':title ,"describle":describle,"article":sgg} );

我想把sgg+item.extract() 转换为字符串.转换为urlcode.发送出去.

不过我碰到问题是这个

articles=response.css("#content > article:nth-child(5) p")

    for item in articles:  print (  type(item.extract()) )

输出
<type 'unicode'>

所以如果

data = {

'title': title,
'describle': describle,
'article': article

}

print urllib.urlencode(data);

就报错了

PHP中文网PHP中文网2811 days ago1030

reply all(1)I'll reply

  • 高洛峰

    高洛峰2017-04-17 17:58:29

    I don’t know what you mean by efficient, please post a more readable code

    articles = response.css("#content > article:nth-child(5) p")
    
    article = ''.join([_.extract() for _ in articles])
    data = {
        'title': title,
        'describle': describle,
        'article': article
    }
    
    print urllib.urlencode(data);

    Please compare this code again

    import urllib
    
    data = {
        'title': '标题',
        'describle': 'abc',
        'article': u'中文'.encode('utf-8')
    }
    
    print urllib.urlencode(data);

    reply
    0
  • Cancelreply