如何使用re.findall提取文本?

2024-10-03 17:27:47 发布

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

我想

  • 从HTML文件中提取国家名称
  • 我从HTML源代码中创建了一个txt文件

我试过的

import re

f = open('/Users/User/Programming/plotly.csv/country_name.txt')
data1 = f.read()
print(re.findall('data_country\=(\".+\")', data1))
print(re.findall('a(.*)b', 'axyzb'))

txt文件如下所示: enter image description here

但代码只给出了这个结果:

enter image description here

我认为这就是正则表达式的问题所在


Tags: 文件代码httpsretxt名称源代码html
2条回答

试试看

print(re.findall('data-country\=(\".+\")', strTmp))

print(re.findall('data-country\=(\".+?\")', data1)) 这正是我需要的

print(re.findall('data-country\=(\".+\")', data1)) 给了我太长的文字,我没有寻找

下次我就不拍截图了。相反,我将复制和文本代码。 谢谢你让我知道

相关问题 更多 >