Python:regex中的名称错误

Python:regex中的名称错误,python,regex,python-3.x,Python,Regex,Python 3.x,我一直在为HTML编写一些正则表达式代码。我用beautiful来表示mist部分,有些部分需要一些正则表达式 代码如下: `import urllib.request from bs4 import BeautifulSoup import re lll='' f=open('n.txt','w') u='http://fortune.com/2015/12/31/wall-street-boutiques-did-well/' r=urllib.request.urlopen(u) s=Be

我一直在为HTML编写一些正则表达式代码。我用beautiful来表示mist部分,有些部分需要一些正则表达式

代码如下:

`import urllib.request
from bs4 import BeautifulSoup
import re
lll=''
f=open('n.txt','w')
u='http://fortune.com/2015/12/31/wall-street-boutiques-did-well/'
r=urllib.request.urlopen(u)
s=BeautifulSoup(r.read(),'html.parser')
x=s.findAll('p')
print(r.read())
for p in x:
        l=str(p)
        ll=re.findall('<p>(.*)<a .*>',l)
        for t in ll:
                l1=t
        ln=re.findall('<a .*>(.*)</a>',l)
        for t in ln:
                l2=t
        lnn=re.findall('</a>(.*)</p>',l)
        for t in ll:
                l3=t
        lll= str(lll)+str(ll)+str(l2)+str(l3)`
re.findall('(.*),l)
没有找到任何匹配项,因此
ll
[]
,这意味着
对于ll中的t:
循环零次,这意味着赋值
l1=t
从未发生过


仔细检查您的正则表达式并更正它,使其与您应用它的内容相匹配。

既然您使用的是Beautifulsoup,那么为什么要使用正则表达式。我只展示了如何实现您想要的输出(即
lll

我使用的是Python 2.7,因此我修改了您的一些代码-这段代码提取了
a
标记内的内容和
p
标记内的内容,并相应地打印出来

from urllib2 import urlopen
from bs4 import BeautifulSoup

u='http://fortune.com/2015/12/31/wall-street-boutiques-did-well/'
r=urlopen(u)
s=BeautifulSoup(r.read(),'html.parser')
x=s.findAll('p')
for i in x:
    if len(i.select('a'))>0:
        print "Inside a {0}".format(''.join([j.text.encode('utf-8') for j in i.select('a')]))
    else:
        print "Inside p {0}".format(i.text.encode('utf-8'))
输出-

Inside p © 2016 Time Inc. All rights reserved.
Inside p In 2015 on Wall Street it appeared that it wasnΓÇÖt the size of the dog in the fight that mattered.
Inside a over $5 trillion of mergers
Inside p Indeed, on nearly all of the biggest deals of the year, Wall StreetΓÇÖs smaller firms nabbed key roles. On PfizerΓÇÖs $160 billion takeover of Allergan, which was the biggest announced acquisition of the year, Centerview Partners, a firm formed by ex-UBS banker Blair Effron and merger titan Robert A. Pruzan less than 10 years ago (currently with about 30 bankers), and Guggeheim Partners, another firm generally considered one of Wall StreetΓÇÖs so-called M&A boutiques, were advisors. Robey Warshaw, an advisory firm formed in spring 2014 with only nine bankers, according to reports, was an advisor on both Anheuser-Busch InBevΓÇÖs takeover of SABMiller and Royal Dutch ShellΓÇÖs announced deal to buy BG Group, which was the second and third largest deals of the year, respectively.
Inside a 
gs


jpm


Inside p Still the boutiquesΓÇÖ share is twice as high as what they took in 2008, according to Dealogic, which defines ΓÇ£boutiquesΓÇ¥ as financial firms with less than 1000 employees which make more than 80% of their revenue from advising on corporate combinations.
Inside p (Dealogic actually excludes Allen & Co. and Guggenheim, because they have sizable stock market businesses, suggesting the results for boutiques should have been even higher.)
Inside p This share has grown steadily since the financial crisis. But market share gains of the smaller firms are particularly surprising this year. Most large deals require financing. So you would expect the big banks to land most of the key roles. But that wasnΓÇÖt the case this year.
Inside p Indeed, about a decade ago, it was believed that the large banks, like Bank of America, Citigroup and JPMorgan Chase, would take an increasingly larger share of the mergers and acquisition by offering cheap loans in return for prized roles on M&A transactions. M&A advisory work is some of the most lucrative on Wall Street. But the financial crisis gave big banks a bad rap. Some clients thought the banks were putting their own interest first, and were turned off by the big banksΓÇÖ ΓÇ£cross-sellΓÇ¥ tactics. As a result, small firms that donΓÇÖt suffer from that stigma have been able to land roles on huge deals. Boutiques also donΓÇÖt have the same regulatory red tape as large banks. WhatΓÇÖs more, some bankers say the advisory job is simply more fun, and, as a result, many boutiques have pulled successful bankers away from behemoths.
Inside p But thereΓÇÖs a downside, though. Unlike large banks, these firms donΓÇÖt have other revenue streams to fall back on when mergers take an inevitable downturn. They may find that they, too, have to grow into other banking areas. Also itΓÇÖs harder to make money when the M&A market turns if a bank depends solely on a few big deals. After all, big deals pay more in fees proportionately, despite that advising small deals requires about the same amount of staff. A firm of a few hundred advisors can either live on a handful of large deals or dozens of small ones.
Inside a 
ghl.........................................
....................................................

我认为正则表达式应该是
'(.*)]*>'
或lazy
(.*)
re.findall('(.*),hello')
产生
['hello']
,所以我不知道这是什么。这可能是一个好主意,这样正则表达式就不会从第一个
p
标记一直匹配到最后一个
a
标记(除非这是OP想要的),但是如果有合适的匹配内容字符串,当前正则表达式不应该导致这个特殊问题。糟糕的变量名。
Inside p © 2016 Time Inc. All rights reserved.
Inside p In 2015 on Wall Street it appeared that it wasnΓÇÖt the size of the dog in the fight that mattered.
Inside a over $5 trillion of mergers
Inside p Indeed, on nearly all of the biggest deals of the year, Wall StreetΓÇÖs smaller firms nabbed key roles. On PfizerΓÇÖs $160 billion takeover of Allergan, which was the biggest announced acquisition of the year, Centerview Partners, a firm formed by ex-UBS banker Blair Effron and merger titan Robert A. Pruzan less than 10 years ago (currently with about 30 bankers), and Guggeheim Partners, another firm generally considered one of Wall StreetΓÇÖs so-called M&A boutiques, were advisors. Robey Warshaw, an advisory firm formed in spring 2014 with only nine bankers, according to reports, was an advisor on both Anheuser-Busch InBevΓÇÖs takeover of SABMiller and Royal Dutch ShellΓÇÖs announced deal to buy BG Group, which was the second and third largest deals of the year, respectively.
Inside a 
gs


jpm


Inside p Still the boutiquesΓÇÖ share is twice as high as what they took in 2008, according to Dealogic, which defines ΓÇ£boutiquesΓÇ¥ as financial firms with less than 1000 employees which make more than 80% of their revenue from advising on corporate combinations.
Inside p (Dealogic actually excludes Allen & Co. and Guggenheim, because they have sizable stock market businesses, suggesting the results for boutiques should have been even higher.)
Inside p This share has grown steadily since the financial crisis. But market share gains of the smaller firms are particularly surprising this year. Most large deals require financing. So you would expect the big banks to land most of the key roles. But that wasnΓÇÖt the case this year.
Inside p Indeed, about a decade ago, it was believed that the large banks, like Bank of America, Citigroup and JPMorgan Chase, would take an increasingly larger share of the mergers and acquisition by offering cheap loans in return for prized roles on M&A transactions. M&A advisory work is some of the most lucrative on Wall Street. But the financial crisis gave big banks a bad rap. Some clients thought the banks were putting their own interest first, and were turned off by the big banksΓÇÖ ΓÇ£cross-sellΓÇ¥ tactics. As a result, small firms that donΓÇÖt suffer from that stigma have been able to land roles on huge deals. Boutiques also donΓÇÖt have the same regulatory red tape as large banks. WhatΓÇÖs more, some bankers say the advisory job is simply more fun, and, as a result, many boutiques have pulled successful bankers away from behemoths.
Inside p But thereΓÇÖs a downside, though. Unlike large banks, these firms donΓÇÖt have other revenue streams to fall back on when mergers take an inevitable downturn. They may find that they, too, have to grow into other banking areas. Also itΓÇÖs harder to make money when the M&A market turns if a bank depends solely on a few big deals. After all, big deals pay more in fees proportionately, despite that advising small deals requires about the same amount of staff. A firm of a few hundred advisors can either live on a handful of large deals or dozens of small ones.
Inside a 
ghl.........................................
....................................................