I am new to python. I have this webpage containing the contents:
<Response>
<Value type="ABC">107544</Value>
<Value type="EFG">10544</Value>
<Value type="ABC">77544</Value>
I would like to parse lines containing ABC and store only the numbers within a temporary text file. How can I do this?
Currently I have
htmlpage = urllib2.urlopen(<URL>)
result = htmlpage.read()
Put your result into BeautifulSoup
, and you will be able to extract any data very easily without regex
from bs4 import BeautifulSoup
result = '''<div class="test">
<a href="example">Result 1</a>
</div>
<div class="test">
<a href="example2">Result 2</a>
</div>'''
soup = BeautifulSoup(result)
for div in soup.findAll('div', attrs={'class':'test'}):
print div.find('a').text
Result 1
Result 2
我将建议使用BeutifulSoup来解析HTML,但是如果您坚持使用正则表达式,则可以尝试以下操作:
re.findall('(?<=type="ABC">).+?(?=<\/)', text, re.S)
Or lxml and xpaths
>>>from lxml import html
>>>result = html.fromstring('''<Response>
<Value type="ABC">107544</Value>
<Value type="EFG">10544</Value>
<Value type="ABC">77544</Value></Response>''')
>>>result.xpath('//value[@type="ABC"]/text()')
...['107544', '77544']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.