1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > QQ好友列表获取导出类:爬虫爬取QQ号

QQ好友列表获取导出类:爬虫爬取QQ号

时间:2022-07-15 16:43:08

相关推荐

QQ好友列表获取导出类:爬虫爬取QQ号

QQ好友列表获取类:爬虫爬取QQ号,这个个爬虫是拿来练手的,可以爬取网络中的QQ号,然后存储到本地

import urllib.requestimport sslimport reimport osfrom collections import deque #导入队列库def writeFileBytes(htmlBytes,topath):with open(topath,'wb') as f:f.write(htmlBytes)def writeFileStr(htmlBytes,topath):with open(topath, 'w') as f:f.write(str(htmlBytes))def QQCrawler(url, topath):headers = {"User-Agent": "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)"}#修改一下浏览器表头(User-Agent)req = urllib.request.Request(url, headers=headers) # 请求体context = ssl._create_unverified_context()#加上此行可以爬取https格式response = urllib.request.urlopen(req, context=context)HTMLStr = response.read()# writeFileBytes(HTMLStr,r"G:\python代码\爬虫\QQ号.html")# writeFileStr(HTMLStr,r"G:\python代码\爬虫\QQ号.txt")htmlStr=str(HTMLStr)pat=r'\b(([\w-]+://?|www[.])[^\s()<>]+(?:[\w\d]+[\w\d]+|([^[:punct:]\s]|/)))' #网址的正则表达式re_url=pile(pat)urlsList=re_url.findall(htmlStr)urlsList = list(set(urlsList))#找到所有的QQpat=r"[1-9][\d]{5,9}" #QQ号的正则表达式re_q=pile(pat) #编译qqList=re_q.findall(htmlStr)qqList=list(set(qqList)) #过滤重复QQ号f=open(topath,'a')#写入文件for qq in qqList:f.write(qq+"\n")f.close()return urlsList #返回一个存储这个网页中的超链接#队列,得到一页中的网址,然后加入队列,爬取每个网页的QQ号def center(url,topath):queue=deque()queue.append(url)while len(queue)!=0:targetUrl=queue.popleft()urlList=QQCrawler(targetUrl, path)for item in urlList:tempUrl=item[0]queue.append(tempUrl)url = r"/group/topic/17359302/" #爬取链接path = r"G:\python代码\爬虫\QQ号.txt" #存储路径# QQCrawler(url,path)center(url,path)

还存在一些问题:url的正则表达式存在问题,不能匹配所有的链接。运行时也存在问题,爬取到本地的数据只有2M多,程序就停止运行了。更多请看:qq好友列表获取软件

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。