我想通过网站scrapy获取文字。这是示例代码:
def parse(self, response):
for kamusset in response.css("div#d1"):
text = kamusset.css("div b::text").extract()
print(dict(text=text))
我想删除'。'符号和每个数字符号。所以,我使用正则表达式。我改变了我的代码:
def parse(self, response):
for kamusset in response.css("div#d1"):
text = kamusset.css("div b::text").re(r'[a-z]+')
print(dict(text=text))
我不希望这样的结果。我想这样:
{' text':[' abadi',' mengabadi',' mengabadikan',' pengabadian', ' keabadian']}。怎么做?
答案 0 :(得分:1)
您可以使用text
re
解析
import re
text = ['aba.di','meng.a.ba.di','megn.a.ba.di.kan','1','2','peng.a.ba.di.an','ke.a.ba.di.an','1','2']
stack = [re.sub('[^a-zA-Z]+', '', e) for e in text]
text_new = [i for i in stack if i!=""]
print(text_new)
text_new将是:
['abadi', 'mengabadi', 'megnabadikan', 'pengabadian', 'keabadian']