[英]how to remove first characters of an item within array in python scrapy
我正在尝试删除数组中某个项目的前7个字符,更具体地说,我正在尝试删除“ mailto”,因此它将仅显示电子邮件
我以为使用[:7]可以解决问题,但是python忽略了该请求。
有什么建议么 ?
def businessprofile(self, response):
for business in response.css('header#main-header'):
item = Item()
item['business_name'] = business.css('div.sales-info h1::text').extract()
item['website'] = business.css('a.secondary-btn.website-link::attr(href)').extract()
# i want to remove the first 7 characters "mailto:", but not sure how ? i made an attempt
item['email'] = business.css('a.email-business::attr(href)').extract()[7:]
item['phonenumber'] = business.css('p.phone::text').extract_first()
for x in item['business_name']:
#new code here, call to self.seen_business_names
if x not in self.seen_business_names:
if item['business_name']:
if item['phonenumber']:
if item['email']:
yield item
self.seen_business_names.append(x)
这是我需要删除字符的地方
item['email'] = business.css('a.email-business::attr(href)').extract()[7:]
显然business.css('a.email-business::attr(href)').extract()
返回一个列表。 您需要从列表中的项目中删除mailto:
s = business.css('a.email-business::attr(href)').extract()
item['email'] = [item[7:] for item in s]
# ['businessname@gmail.com']
要么
s = business.css('a.email-business::attr(href)').extract()
item['email'] = [item.replace('mailto:', '') for item in s]
# ['businessname@gmail.com']
您需要使用[7:]
而不是[:7]
语法为[<start>:<end>]
,如果省略,它将自动从字符串的开头或结尾开始。
例如:
val = "mailto:abc@abc.de"
mailto = val[:7] # from first charater to 7th = 'mailto:'
email = val[7:] # 8th character to the end.
计数从0开始:
a = "0123456789"
a[7:]
# '789'
所以你可能需要
a[8:]
# '89'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.