字符串splitlines()
描述
Python splitlines() 按照行(’\r’, ‘\r\n’, \n’)分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。
语法
splitlines()方法语法:
str.splitlines([keepends])
参数
keepends – 在输出结果里是否去掉换行符(’\r’, ‘\r\n’, \n’),默认为 False,不包含换行符,如果为 True,则保留换行符。
返回值
返回一个包含各行作为元素的列表。
获取url_demo:
将浏览器的请求头转换成字符串,通过splintlines()方法得到列表,提取url
def main():
name = """
GET / HTTP/1.1
Host: 127.0.0.1:9999
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0 .8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Geck o) Chrome/50.0.2661.102 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN,zh;q=0.8
GET /favicon.ico HTTP/1.1
Host: 127.0.0.1:9999
Connection: keep-alive
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Geck o) Chrome/50.0.2661.102 Safari/537.36
Accept: */*
Referer: http://127.0.0.1:9999/
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN,zh;q=0.8
"""
# str.splitlines([keepends])
# keepends -- 在输出结果里是否去掉换行符('\r', '\r\n', \n'),默认为 False,不包含换行符,如果为 True,则保留换行符。
str1 = name.splitlines(True)
print("保留换行符结果:\n",str1)
str2 = name.splitlines(False)
print("不保留换行符结果:\n",str2)
if __name__ == '__main__':
main()
运行结果
得到的是以字符串为元素的列表,就可以通过获取列表元素,正则匹配到url
保留换行符结果:
[’\n’, ‘\tGET / HTTP/1.1\n’, ‘\tHost: 127.0.0.1:9999\n’, ‘\tConnection: keep-alive\n’, ‘\tCache-Control: max-age=0\n’, ‘\tAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0 .8\n’, ‘\tUpgrade-Insecure-Requests: 1\n’, ‘\tUser-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Geck o) Chrome/50.0.2661.102 Safari/537.36\n’, ‘\tAccept-Encoding: gzip, deflate, sdch\n’, ‘\tAccept-Language: zh-CN,zh;q=0.8\n’, ‘\n’, ‘\tGET /favicon.ico HTTP/1.1\n’, ‘\tHost: 127.0.0.1:9999\n’, ‘\tConnection: keep-alive\n’, ‘\tUser-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Geck o) Chrome/50.0.2661.102 Safari/537.36\n’, ‘\tAccept: /\n’, ‘\tReferer: http://127.0.0.1:9999/\n’, ‘\tAccept-Encoding: gzip, deflate, sdch\n’, ‘\tAccept-Language: zh-CN,zh;q=0.8\n’, ‘\n’, ‘\t’]
不保留换行符结果:
[’’, ‘\tGET / HTTP/1.1’, ‘\tHost: 127.0.0.1:9999’, ‘\tConnection: keep-alive’, ‘\tCache-Control: max-age=0’, ‘\tAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0 .8’, ‘\tUpgrade-Insecure-Requests: 1’, ‘\tUser-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Geck o) Chrome/50.0.2661.102 Safari/537.36’, ‘\tAccept-Encoding: gzip, deflate, sdch’, ‘\tAccept-Language: zh-CN,zh;q=0.8’, ‘’, ‘\tGET /favicon.ico HTTP/1.1’, ‘\tHost: 127.0.0.1:9999’, ‘\tConnection: keep-alive’, ‘\tUser-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Geck o) Chrome/50.0.2661.102 Safari/537.36’, ‘\tAccept: /’, ‘\tReferer: http://127.0.0.1:9999/’, ‘\tAccept-Encoding: gzip, deflate, sdch’, ‘\tAccept-Language: zh-CN,zh;q=0.8’, ‘’, ‘\t’]
参考资料:
http://www.runoob.com/python3/python3-string-splitlines.html