python 正则表达式提取url
原创
©著作权归作者所有:来自51CTO博客作者wx5861be15b7630的原创作品,请联系作者获取转载授权,否则将追究法律责任
1. python 提取URL到数组中
import re
if __name__ == "__main__":
f = open(r"C:\Users\HuJun\PycharmProjects\pythonProject\daily_tesy\data.txt", "r", encoding="utf-8")
data = f.read()
reg = 'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
# reg = 'https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+'
url = re.findall(reg, data)
print(url)
2. python使用正则表达式提取网页URL的方法
import re
import urllib
if __name__ == "__main__":
url = "https://www.jb51.net"
res = urllib.urlopen(url).read()
data = res.replace(" ", "")
urls = re.findall(r"<a.*?href=.*?<\/a>", data, re.I)
for i in urls:
print(1)
else:
print('this is over')