Python 将列表转换为字符串,同时逐字保存
很沮丧地说我在这件事上被难住了。我正在从段落中提取文本:Python 将列表转换为字符串,同时逐字保存,python,regex,python-2.7,Python,Regex,Python 2.7,很沮丧地说我在这件事上被难住了。我正在从段落中提取文本: paragraphs = re.findall(r'(<p(.*?)</p>)', html) …我一个字母接一个字母地收到这篇文章,这些单词都被分开了。我是Python新手,这让我很困惑 第一个问题:为什么“段落”不以字符串开头 第二个问题:如何将“段落”转换为字符串,逐字保存,例如: paragraph = ['Two', 'words'] re.findall()返回匹配项列表。您需要re.se
paragraphs = re.findall(r'(<p(.*?)</p>)', html)
…我一个字母接一个字母地收到这篇文章,这些单词都被分开了。我是Python新手,这让我很困惑
第一个问题:为什么“段落”不以字符串开头
第二个问题:如何将“段落”转换为字符串,逐字保存,例如:
paragraph = ['Two', 'words']
re.findall()
返回匹配项列表。您需要re.search()
不过,更好的选择是使用HTML解析器,如:
>>来自bs4导入组
>>>
>>>数据=“这里有一些文本”
>>>soup=BeautifulSoup(数据,“html.parser”)
>>>soup.p.get_text().split()
[u'some',u'text',u'here']
re.findall()
返回匹配项列表。您需要re.search()
不过,更好的选择是使用HTML解析器,如:
>>来自bs4导入组
>>>
>>>数据=“这里有一些文本”
>>>soup=BeautifulSoup(数据,“html.parser”)
>>>soup.p.get_text().split()
[u'some',u'text',u'here']
paragraphs = str(paragraphs)
paragraph = ['Two', 'words']
>>> from bs4 import BeautifulSoup
>>>
>>> data = '<p>some text here</p>'
>>> soup = BeautifulSoup(data, "html.parser")
>>> soup.p.get_text().split()
[u'some', u'text', u'here']