|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
各位老鱼油,前辈、大神,新手上路,希望多多指导:
源代码:
import urllib.request as request
import re
def openurl(url):
response = request.Request(url)
agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2141.400 QQBrowser/9.5.10219.400'
response.add_header('User-Agent',agent)
html = request.urlopen(response)
html = html.read().decode('utf-8')
return html
def getip(html):
#第一种格式
#p = re.compile(r'(?:(?1|:[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}(?:1|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])')
#第二种格式
p = re.compile(r'(?:(?:[0,1]?\d?\d|2[0-4]\d|25[0-5])\.){3}(?:[0,1]?\d?\d|2[0-4]\d|25[0-5])')
result = p.findall(html)
count = 0
for ip in result:
print(ip)
count += 1
print(count)
def loadip():
url = 'http://www.xicidaili.com/wn/1'
html = openurl(url)
getip(html)
loadip()
问题:
我看了下两种匹配模式,新手认为第一种模式和第二种模式的效果是一样的但是结果却完全不一致,而且用第一种模式匹配出来的IP地址有点奇怪。经过试验验证,新手认为第二种格式是对的,但是却不知道第一种格式是什么原因导致失败,找不出问题所在,为什么会有区别,希望各位鱼油老师,大神指点,谢谢 |
|