Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python HTML文本提取_Python_Html_Beautifulsoup - Fatal编程技术网

Python HTML文本提取

Python HTML文本提取,python,html,beautifulsoup,Python,Html,Beautifulsoup,我有一个清单,如下所示,来自美丽的汤 soup = BeautifulSoup(page.content, 'html.parser') area = soup.select("td strong") 比如说 area=[ <strong><span style="font-size:1.4em;">120 Beats Per Minute (15)</span><br/><br/>Cinema</strong>, &l

我有一个清单,如下所示,来自美丽的汤

soup = BeautifulSoup(page.content, 'html.parser')
area = soup.select("td strong")
比如说

area=[
<strong><span style="font-size:1.4em;">120 Beats Per Minute (15)</span><br/><br/>Cinema</strong>, 
<strong><span style="font-size:1.4em;">A Little Night Music</span><br/><br/>Theatre</strong>, 
<strong><span style="font-size:1.4em;">A Wrinkle in Time (PG)</span><br/><br/>Cinema</strong>
]
区域=[
每分钟120次(15)

一点夜音乐

剧院, 时间上的皱纹(PG)

]
除了电影、戏剧,我需要摆脱文字

我想出了下面的表达式,但我不能把它应用到列表中

x[x.find('<br/><br/>')+10:].replace('</strong>','')
x[x.find('

')+10://替换('
','')
你知道我如何应用这个表达式从列表中提取数据来创建一个新的列表吗?我试过这个:

clean_area=[]
for x in area:
   clean_area.append(x[x.find('<br/><br/>')+10:].replace('</strong>',''))
clean_area=[]
对于区域中的x:
清理区域。追加(x[x.find('

')+10:://替换('
','')
但我得到了这个错误:
TypeError:不支持+:'NoneType'和'int'的操作数类型。

大约一小时前,我回答了你的第一篇帖子,但你删除了它

我不确定这是否是最好的方法,但以下是我的想法:

text = [
"""<strong><span style="font-size:1.4em;">120 Beats Per Minute (15)</span><br/><br/>Cinema</strong>""", 
"""<strong><span style="font-size:1.4em;">A Little Night Music</span><br/><br/>Theatre</strong>""", 
"""<strong><span style="font-size:1.4em;">A Wrinkle in Time (PG)</span><br/><br/>Cinema</strong>"""
]

text = ''.join(text) #Converting list of strings to one string

start = "<br/><br/>" #Start indication
end = "</" #End indication

clean_area = []

index = 0
while index < len(text):
    index = text.find(start, index)
    if index == -1:
        break
    clean_area.append(text[index+len(start):text.find(end, index)])
    index += len(start)

print(clean_area)
text=[
“每分钟120次(15次)

, “一点夜音乐

剧院”“, “时间上的皱纹(PG)

] text=''.join(text)#将字符串列表转换为一个字符串 start=“

”启动指示
end=“您想要使用的是
分解
,这将删除您不想要的任何标记

在这种情况下,它是
span

所以

返回


电影院、剧院

我只能通过两次才能做到这一点。我确信这不是最好的方法,但至少是有效的

soup = BeautifulSoup(result.content, "html.parser")

for x in soup.findAll("span"):
    x.decompose()

area = soup.select("td strong")

a = str(area)
soup2 = BeautifulSoup(a)



tr = []
for tag in soup2.find_all(True):
    tr.append(tag.text)


clean_area = [] 
for i in tr[::3]:
    clean_area.append(i)

这个错误是说
x.find(“

”)
如果我手动将一个列表元素复制/粘贴到字符串x中,则找不到任何内容。我的表达式工作正常。可能其他区域字符串中的一个没有两个断点?我建议实际解析文本,而不是查找子标签。当文本被合并时,我不能这样做-我需要将标签和文本转储到正常列表中,以便我可以操作数据。有什么想法吗?我的文本在一个一维列表中,每个元素都对应于你答案中的文本字符串。我如何用你的代码在列表中循环?你可以通过执行area=''将列表变成一个字符串。加入(area)。我将编辑我的答案,用你的示例来展示它
soup = BeautifulSoup(result.content, "html.parser")

for x in soup.findAll("span"):
    x.decompose()

area = soup.select("td strong")

a = str(area)
soup2 = BeautifulSoup(a)



tr = []
for tag in soup2.find_all(True):
    tr.append(tag.text)


clean_area = [] 
for i in tr[::3]:
    clean_area.append(i)