Python爬虫爬取数据到sqlite数据库实例
萌新:使用xpath和正则表达式解析网页内容
代码如下:
import sqlite3
import re
import requests
from lxml import htmlfindlink = re.compile(r'<a href="(.*?)"') # 创建正则表达式对象,表示规则(字符串的模式)
findname = re.compile(r'<a href=".*?">(.*?)</a>')
findname2 = re.compile(r'<td style="outline: 0px !important;">(.*?)</td>')
findname3 = re.compile(r'<td style="outline: 0px !important;"><p style="line-height: 1.8; outline: 0px !important;">(.*?)</p></td>')
findname4 = re.compile(r'<td style="outline: 0px !important;"><p style="line-height: 1.8; outline: 0px !important;"><a href=".*?">(.*?)</a>.*?</p></td>')
findaddres = re.compile(r'<td style="outline: 0px !important;">(.*?)</td>')
findadress1 = re.compile(r'<td style="outline: 0px !important;"><a href=".*?">(.*?)</a></td>')'''
通过findall找到所有table里面的tr
然后对tr里面的内容进行解析,如果没有链接,则data添加信息为空,有链接调用函数来解析链接网页
再向数据库传输解析内容
'''def main():basicurl = "http://www.qianmu.org/ranking/1528.htm"datalist = getData(basicurl)for data in datalist:print(data)saveDatadb(datalist,"university.db")# 得到一个指定的网页内容
def askURL(url):et = html.etreerespon = requests.get("http://www.qianmu.org/ranking/1528.htm")selector = et.HTML(respon.text)return selector# 爬取主网页,将网页的tr提取出来进行分析
def getData(basicurl):datalist = []selector = askURL(basicurl)# 找出每个tr,对每个tr解析trs = selector.xpath('//div[@class="rankItem"]//tr[position()>1]')# names = selector.xpath('//div[@class="rankItem"]//tr[position()>1]/td/a/text() | //div[@class="rankItem"]//tr['# 'position()>1]/td[2]/text()')# links = selector.xpath('//div[@class="rankItem"]//tr[position()>1]/td/a/@href')# 获得了每一个tr内容for tr in trs:data = []tr = html.tostring(tr, encoding='utf-8').decode('utf-8')name = re.findall(findname, tr)name1 = re.findall(findname2, tr)if len(name) == 0:name = name1[1]else:name = name[0]data.append(name)# 获取英文名字if len(re.findall(findname4, tr)) > 1 or len(re.findall(findname4, tr)) == 1:english = ''.join(re.findall(findname4, tr)[0])else:english = re.findall(findname3, tr)[1]data.append(english)if len(re.findall(findadress1, tr)) > 1:address = ''.join(re.findall(findadress1, tr)[1])else:address = re.findall(findaddres, tr)[3]data.append(address)link = re.findall(findlink, tr)# if len(link) > 1:# link = link[0]# elif len(link) == 0:# link = ' '# else:# link = ''.join(link)# 开始对link进行分析if len(link) > 1:link = link[0]elif len(link) == 0:link = ' 'else:link = ''.join(link)data.append(link)datalist.append(data)return datalist# 保存数据
def saveDatadb(datalist, dbpath):init_db(dbpath)conn = sqlite3.connect(dbpath)cur = conn.cursor() # 获取游标# print("我执行了")for data in datalist:for index in range(len(data)):data[index] = '"' + str(data[index]) + '"' # '"'+data[index]+'"'sql = '''insert into university(name, ename, address, link) values (%s)''' % ",".join(data)# print(sql)cur.execute(sql)conn.commit() # 提交cur.close()conn.close() # 关闭链接# 创建数据库
def init_db(dbpath):sql = '''create table university(id integer primary key autoincrement,name text ,ename text ,address text ,link text);'''conn = sqlite3.connect(dbpath) # 建表cursor = conn.cursor() # 游标cursor.execute(sql) # 执行sql语句建表conn.commit() # 提交conn.close() # 关闭if __name__ == "__main__": # 当程序执行时,调用函数 这样写的目的是严格控制函数执行的主流程main()
执行后保存的数据结果:
因有几个name正则表达式未处理妥当,可能会出现错误,可以自行调试,谅解,我懒。
Python爬虫爬取数据到sqlite数据库实例相关推荐
- Python爬虫---爬取数据(上)
又是女票,拿了3万多条的13年某地区的公司信息,但是三年过去了,工商局的注册信息发生了巨大变化,有的注册资本增加了,有的公司老板变多了,更有不少公司不存在了,因此,需要一份最新的信息以便于她进行使用. ...
- 如何用六步教会你使用python爬虫爬取数据
前言: 用python的爬虫爬取数据真的很简单,只要掌握这六步就好,也不复杂.以前还以为爬虫很难,结果一上手,从初学到把东西爬下来,一个小时都不到就解决了. python爬出六部曲 第一步:安装req ...
- python爬虫代码实例-Python爬虫爬取百度搜索内容代码实例
这篇文章主要介绍了Python爬虫爬取百度搜索内容代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 搜索引擎用的很频繁,现在利用Python爬 ...
- 采用python爬虫爬取数据然后采用echarts数据可视化分析
前言:采用python爬虫爬取天气数据然后采用echarts数据可视化画图分析未来天气变化趋势 从获取数据到可视化的流程 (熟悉爬虫爬取数据然后数据可视化这个过程) 一.python爬取未来8-15天 ...
- 爬虫python的爬取步骤-Python爬虫爬取数据的步骤
爬虫: 网络爬虫是捜索引擎抓取系统(Baidu.Google等)的重要组成部分.主要目的是将互联网上的网页下载到本地,形成一个互联网内容的镜像备份. 步骤: 第一步:获取网页链接 1.观察需要爬取的多 ...
- Python爬虫爬取数据的步骤
爬虫: 网络爬虫是捜索引擎抓取系统(Baidu.Google等)的重要组成部分.主要目的是将互联网上的网页下载到本地,形成一个互联网内容的镜像备份. 步骤: 第一步:获取网页链接 1.观察需要爬取的多 ...
- python爬虫步骤-Python爬虫爬取数据的步骤
爬虫: 网络爬虫是捜索引擎抓取系统(Baidu.Google等)的重要组成部分.主要目的是将互联网上的网页下载到本地,形成一个互联网内容的镜像备份. 步骤: 第一步:获取网页链接 1.观察需要爬取的多 ...
- Python爬虫爬取数据
我是Python小白,我会用比较通俗易懂的方法告诉你如何去爬取数据. 一开始,我们需要pycharm(也就是我们编代码的工具),其次我们需要打开我们需要爬取数据的网页,我以鞋子为例. 那么,接下来就开 ...
- python爬虫爬取数据遇到的问题
自学爬虫中遇到的一点问题和部分解决方法 本人大一新手,学python两个月,目前都是在网上找爬虫教学尝试自学(最初的目的是爬美女图片) 最开始接触爬虫的时候是一个前辈给我的代码和一个api的网站聚合数 ...
最新文章
- 用户权限_SAP 用户权限
- 使用metasploit收集邮箱
- 打成jar包_keras, tensorflow模型部署通过jar包部署到spark环境攻略
- 美国星巴克肯德基明年将支持Apple Pay
- 在.NET中使用DiagnosticSource
- 云计算未来趋势预测:AIaaS、无服务器、云端一体化等将成重点?
- 优秀!师兄妹齐发Science,师妹22岁担任一作!同为曹原中科大校友
- linux nginx F配置,linux下nginx的安装及配置
- hibernate学习内容
- 手机MODEM 开发(28)--- VoLTE介绍
- 实现indexof()
- STM32工作笔记0064---输入捕获实验
- [JS]关于表单的自动运算(已解决)
- 基于随机森林的姿态识别算法
- 计算机硬盘通过usb接口,并口硬盘转usb接口的方法介绍【详解】
- CentOS7--安装谷歌浏览器--详细步骤
- 用c语言写图书馆程序,用c语言写图书馆管理系统
- 如何在Cent OS7安装jdk
- 视频编解码 — DCT变换和量化
- 出生日期格式正则表达式