Python爬取海安网所有号码
来源:互联网 发布:数据采集系统标准规范 编辑:程序博客网 时间:2024/04/28 16:55
注:1.本程序采用MS SQL server数据库,请运行前手动修改程序开始处的数据库连接信息。
2.需要pyodbc库的支持
import requests,re,sysfrom bs4 import BeautifulSoupimport pyodbcpages = set()conn = pyodbc.connect(r'DRIVER={SQL Server};SERVER=WXS-PC;DATABASE=Test;UID=sa')cursor = conn.cursor()#递归爬取整个网站,并将链接存入集合def getLinks(pageUrl): global pages r = requests.get(pageUrl , timeout =30) demo = r.text bsObj = BeautifulSoup(demo,'html.parser') #去除外链 for link in bsObj.findAll('a',href=re.compile("(www.haianw.com)+")): if 'href' in link.attrs: if link.attrs['href'] not in pages: newPage = link.attrs['href'] findPhone(newPage) pages.add(newPage) getLinks(newPage)#将当前url中的手机号存入字典def findPhone(url): numbers = {} r = requests.get(url,timeout=30) data = r.text #电话号码和手机号码正则表达式 phone_list = re.findall(r"\d{3}-\d{8}|\d{4}-\d{7}|1[34578]\d{9}",data) phone_list = list(set(phone_list)) for phone in phone_list: numbers[phone] = url writePhone(numbers) def writePhone(numbers): global cursor global conn for k,v in numbers.items(): temp = "insert into Numbers (link,number) values ('{}','{}')".format(v,k) cursor.execute(temp) conn.commit() if __name__ == '__main__': #设置递归深度为一百万,防止爬虫崩溃 sys.setrecursionlimit(1000000) print("开始爬取全站链接...") try: getLinks('http://www.haianw.com') except Exception: print('爬虫发生崩溃错误,已停止爬取...')
0 0
- Python爬取海安网所有号码
- 使用Python对双色球号码进行爬取
- Python 爬取百度网盘所有热门分享文件
- 爬虫实战一:爬取当当网所有 Python 书籍
- [python] 爬取网站所有的URL
- [python] 爬取网站所有的URL
- Python实现人人网爬虫,爬取用户所有状态信息。
- 使用python爬取全国所有热门景点数据---去哪儿网
- python 爬虫爬取人人网你的好友的所有相册图片
- python爬虫爬取csdn博客专家所有博客内容
- python爬取并下载麦子学院所有视频教程
- python 爬虫爬取所有上市公司公告信息(一)
- python 爬虫爬取所有上市公司公告信息(二)
- python 爬虫爬取所有上市公司公告信息(三)
- python 爬虫爬取所有上市公司公告信息(四)
- python 爬虫爬取所有上市公司公告信息(五)
- 到达海安
- 取图标ICON号码
- :hover放前面和放后面的区别
- Hibernate缓存机制
- $.ajax()详解
- springmvc使用freemarker
- 学习淘淘商城第九课(SSM框架整合之Service层整合)
- Python爬取海安网所有号码
- 退出while循环
- leetcode week3
- Linux内核设备驱动之GIC驱动
- 进程和线程的区别
- 初学Linux(四)—gcc的使用和代码调试
- 手机端实现手机键盘搜索
- android插件化
- 移动端viewport的作用