1.采集函数:根据前后字符串从内容中截取
def cut_str(self,content,start,end):
start_index = content.find(start)
if start_index == -1:
return ""
start_index += len(start)
end_index = content.find(end,start_index)
if end_index == -1:
return ""
return content[start_index:end_index]
2.正则特殊符号转义函数re.escape
import re
pattern = re.escape("http://www.testtest.com")
content = "http://www.testtest.com/abc/"
match_list = re.findall(pattern, content)
print(match_list)
3.获取当前主机IP
def get_local_ip():
hostname = socket.gethostname()
# 根据主机名获取本机IP地址列表
local_ip = socket.gethostbyname(hostname)
return local_ip
- urlencode 和urldecode
#urlencode:
urllib.parse.quote(url, safe='/',encoding='utf-8', errors='replace') #其中safe 指定不应被转码的 ASCII 字符 — 其默认值为"/"
#urldecode
urllib.parse.unquote(url,encoding ='utf-8',errors ='replace')
5 xml和dict互相转换
#xml转dict
xml_dict = xmltodict.parse(xmldata)
#dict转xml
xmldata= xmltodict.unparse(xml_dict, pretty=True)
#pretty=True参数用于在生成的XML中添加缩进和换行符,以提高可读性
6 获取一个URL的根域名
def get_base_url(url):
parsed_url = urlparse(url)
base_url = parsed_url.scheme + '://' + parsed_url.netloc
return base_url
提取Email
def fetch_emails(txt):
email_regex = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
matches = re.findall(email_regex, txt)
return matches