# 开发时间:2021/2/2 22:28
# 开发工具:PyCharm
# 开发者:GoTop
import requests
import redef getPhoto(qqCode):url = f"http://q.qlogo.cn/headimg_dl?dst_uin={qqCode}&spec=640&img_type=jpg"headers = {'Referer': 'http://pic.netbian.com/4kmeinv/index_2.html','user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'}r = requests.get(url = url, headers = headers).contentwith open(qqCode+".jpg", 'wb') as fp:fp.write(r)
def getNickName(qqCode):url = 'http://r.qzone.qq.com/fcg-bin/cgi_get_portrait.fcg?g_tk=1518561325&uins=' + qqCodeheaders = {'Referer': 'http://pic.netbian.com/4kmeinv/index_2.html','user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'}r = requests.get(url = url, headers = headers).textr = r.encode('iso-8859-1').decode('gbk').split(',')[6]r = re.findall("\"(.*?)\"", r)[0]print("NickName: ", r)if __name__ == '__main__':nums = list(map(str, input("请输入QQ号:").split()))for num in nums:getNickName(num)getPhoto(num)
这里采用的是对腾讯的API接口发送GET请求,这样可以获取到QQ号对应的昵称和头像。涉及到了正则表达式提取内容,字符编码,用requests模块编写简单爬虫程序等知识。