Beautifulsoup Parsing- detail info
I already asked a question, but it seems my explnation was not clear.. So, I am asking again with more detail info.
<h2 class="sectionTitle">
CORPORATE HEADQUARTERS </h2>
277 Park Avenue<br />
New York, New York 10172
<br /><br />United States<br /><br />
I would like to extract only New York, New York without pos开发者_如何学编程tal code 10172
And this is another question..
<h2 class="sectionTitle">
BACKGROUND</h2>
He graduated Blabala
</span>
I would like to extract only He graduated Blabla
I have been spending few days, so I feel I could become crazy.. Please help me.. thank you for your kind help in advance.
You still need more detail to write a good regex.
For example, if you want to extract the second line of "CORPORATE HEADQUARTERS" without a postal code that always exists, it can be written like this:
>>> import re
>>> html = '''
... <h2 class="sectionTitle">
... CORPORATE HEADQUARTERS </h2>
... 277 Park Avenue<br />
... New York, New York 10172
... <br /><br />United States<br /><br />
...
... <h2 class="sectionTitle">
... BACKGROUND</h2>
... He graduated Blabala
... </span>
... '''
>>> re.search('(?s)<h2 class="sectionTitle">\s*CORPORATE HEADQUARTERS\s*</h2>.*?<br />([^<>]+) \d+', html).group(1).strip()
'New York, New York'
>>> re.search('(?s)<h2 class="sectionTitle">\s*BACKGROUND\s*</h2>([^<>]+)', html).group(1).strip()
'He graduated Blabala'
You should use a combination of tag.contents
with .split('\n') to split on lines and
.rsplit(' ', 1)` to split only the right most space-separated string.
精彩评论