python - 抓取下的unicode编码转urlcode
PHP中文网
PHP中文网 2017-04-17 17:56:29
0
1
1029
    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中文网

认证高级PHP讲师

reply all(1)
小葫芦

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);
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template