在python中分割unicode字符串的正确方法是什么?

2024-09-27 20:17:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python还不熟悉,正在玩弄这个蹩脚的网络爬虫。 我想抓取描述字符串的前10个字符并将其用作标题

下面的python代码片段生成了下面的JSON

item['image'] = img.xpath('@src').extract()
item_desc = img.xpath('@title').extract()
print(item_desc)
item['description'] = item_desc
item['title'] = item_desc[:10]
item['parentUrl'] = response.url

{'description': [u'CHAR-BROIL Tru-Infrared 350 IR Gas Grill - SportsAuthority.com '],
 'image': [u'http://www.sportsauthority.com/graphics/product_images/pTSA-10854895t130.jpg'],
 'parentUrl': 'http://www.sportsauthority.com/category/index.jsp?categoryId=3077576&clickid=topnav_Jerseys+%26+Fan+Shop',
 'title': [u'CHAR-BROIL Tru-Infrared 350 IR Gas Grill - SportsAuthority.com ']}

我想要的是下面的。切片的行为不符合我的预期。你知道吗

{'description': [u'CHAR-BROIL Tru-Infrared 350 IR Gas Grill - SportsAuthority.com '],
 'image': [u'http://www.sportsauthority.com/graphics/product_images/pTSA-10854895t130.jpg'],
 'parentUrl': 'http://www.sportsauthority.com/category/index.jsp?categoryId=3077576&clickid=topnav_Jerseys+%26+Fan+Shop',
 'title': [u'CHAR-BROIL']}

Tags: imagecomhttpirtitlewwwdescriptionitem
1条回答
网友
1楼 · 发布于 2024-09-27 20:17:23

item_desc是一个列表,其中有一个元素,该元素是一个unicode字符串。它本身不是unicode字符串。[...]是一个很大的暗示。你知道吗

取出元素,切片,然后将其放回列表中:

item['title'] = [item_desc[0][:10]]

显然,.extract()函数可以返回多个匹配项;如果只需要一个匹配项,也可以选择第一个:

item['image'] = img.xpath('@src').extract()[0]
item_desc = img.xpath('@title').extract()[0]
item['description'] = item_desc
item['title'] = item_desc[:10]

如果XPath查询并不总是返回结果,请首先测试空列表:

img_match = img.xpath('@src').extract()
item['image'] = img_match[0] if img_match else ''
item_desc = img.xpath('@title').extract()
item['description'] = item_desc[0] if item_desc else ''
item['title'] = item_desc[0][:10] if item_desc else ''

相关问题 更多 >

    热门问题