我只是想从这样一个网页上获取一些数据:
[ . . . ]
<p class="special-large">Lorem Ipsum 01</p>
<p class="special-large">Lorem Ipsum 02</p>
<p class="special-large">Lorem Ipsum 03</p>
<p class="special-large">Lorem Ipsum 04</p>
<p class="special-large">Lorem Ipsum 05</p>
[ . . . ]
我想要一个类似下面的python数组:
myArrayWebPage = ["Lorem Ipsum 01","Lorem Ipsum 02","Lorem Ipsum 03","Lorem Ipsum 04","Lorem Ipsum 05"]
这是我的python脚本:
import urllib.request
urlAddress = "http:// ... /" # my url address
getPage = urllib.request.urlopen(urlAddress)
outputPage = getPage.read()
print(outputPage)
如何从“outputPage”获取数组?
答案 0 :(得分:1)
这似乎可以满足您的需求:
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> html = '''<p class="special-large">Lorem Ipsum 01</p>
<p class="special-large">Lorem Ipsum 02</p>
<p class="special-large">Lorem Ipsum 03</p>
<p class="special-large">Lorem Ipsum 04</p>
<p class="special-large">Lorem Ipsum 05</p>'''
>>> import re
>>> re.findall('<p class="special-large">([^<]+)</p>', html)
['Lorem Ipsum 01', 'Lorem Ipsum 02', 'Lorem Ipsum 03', 'Lorem Ipsum 04', 'Lorem Ipsum 05']
>>>
请注意regular expressions通常不适用于此类内容。您应该使用Beautiful Soup之类的库。