我有一个使用beautifulsoup从网站上抓取的HTML代码。我想在python中使用正则表达式从html代码中提取一部分网址。这是html的一部分:
<link rel="stylesheet" type="text/css" href="/include/xbrlViewerStyle.css">
<style type="text/css">li.octave {border-top: 1px solid black;}</style>
<!--[if lt IE 8]>
<style type="text/css">
li.accordion a {display:inline-block;}
li.accordion a {display:block;}
</style>
<![endif]-->
<script type="text/javascript" language="javascript">
var InstanceReportXslt = "/include/InstanceReport.xslt";
var reports = new Array(161);
reports[0+1] = "/Archives/edgar/data/49196/000004919618000008/R1.htm";
reports[1+1] = "/Archives/edgar/data/49196/000004919618000008/R2.htm";
reports[2+1] = "/Archives/edgar/data/49196/000004919618000008/R3.htm";
reports[3+1] = "/Archives/edgar/data/49196/000004919618000008/R4.htm";
reports[4+1] = "/Archives/edgar/data/49196/000004919618000008/R5.htm";
reports[5+1] = "/Archives/edgar/data/49196/000004919618000008/R6.htm";
reports[6+1] = "/Archives/edgar/data/49196/000004919618000008/R7.htm";
reports[7+1] = "/Archives/edgar/data/49196/000004919618000008/R8.htm";
reports[8+1] = "/Archives/edgar/data/49196/000004919618000008/R9.htm";
reports[9+1] = "/Archives/edgar/data/49196/000004919618000008/R10.htm";
reports[10+1] = "/Archives/edgar/data/49196/000004919618000008/R11.htm"
我想使用正则表达式来标识“ R4”以提取“ /Archives/edgar/data/49196/000004919618000008/R4.htm”。
答案 0 :(得分:1)
您可以使用以下表达式:
>>> import re
>>> s = '''reports[0+1] = "/Archives/edgar/data/49196/000004919618000008/R1.htm";
... reports[1+1] = "/Archives/edgar/data/49196/000004919618000008/R2.htm";
... reports[2+1] = "/Archives/edgar/data/49196/000004919618000008/R3.htm";
... reports[3+1] = "/Archives/edgar/data/49196/000004919618000008/R4.htm";
... reports[4+1] = "/Archives/edgar/data/49196/000004919618000008/R5.htm";
... reports[5+1] = "/Archives/edgar/data/49196/000004919618000008/R6.htm";
... reports[6+1] = "/Archives/edgar/data/49196/000004919618000008/R7.htm";
... reports[7+1] = "/Archives/edgar/data/49196/000004919618000008/R8.htm";'''
>>> for i in re.findall(r'([\w./]+R4[\w./]+)', a):
... print(i)
...
/Archives/edgar/data/49196/000004919618000008/R4.htm