python开发简单爬虫
2016-10-31 01:19
295 查看
确定目标:哪个网站哪些网页哪部分数据
分析目标:制定抓取策略——url格式、数据格式、网页编码
编写代码:使用抓取策略结果
使用爬虫抓取
1.要注意Python2与Python3的编解码区别
及时关闭链接否则会造成资源被占用,降低性能。
生成爬虫以后,将抓取到的数据插入到数据库中,将主键值设定为自增长插入部分字段即可
代码如下
import pymysql
# conn = pymysql.connect(host='127.0.0.1', user='root', passwd='root', db='crawler')
class htmlpymysql(object):
def insert(self,url, title,summary):
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='crawler',charset='utf8mb4')
cur = conn.cursor()
cur.execute("INSERT INTO craw(url, title,summary) VALUES(%s, %s,%s)",(url, title,summary))
conn.commit()
# print cur.description
cur.close()
conn.close()
数据库形式如图
分析目标:制定抓取策略——url格式、数据格式、网页编码
编写代码:使用抓取策略结果
使用爬虫抓取
1.要注意Python2与Python3的编解码区别
def output_html(self): fout = open('output.html','w',encoding='utf-8') fout.write("<html> ") fout.write("<meta charset='utf-8'>") fout.write("<body>") fout.write("<table>") for data in self.datas: fout.write("<tr>") fout.write("<td>%s</td>" % data['url']) fout.write("<td>%s</td>" % data['title']) fout.write("<td>%s</td>" % data['summary']) fout.write("</tr>") fout.write("</table>") fout.write("</body>") fout.write("</html>") fout.close()此为Python3编码方式
及时关闭链接否则会造成资源被占用,降低性能。
生成爬虫以后,将抓取到的数据插入到数据库中,将主键值设定为自增长插入部分字段即可
代码如下
import pymysql
# conn = pymysql.connect(host='127.0.0.1', user='root', passwd='root', db='crawler')
class htmlpymysql(object):
def insert(self,url, title,summary):
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='crawler',charset='utf8mb4')
cur = conn.cursor()
cur.execute("INSERT INTO craw(url, title,summary) VALUES(%s, %s,%s)",(url, title,summary))
conn.commit()
# print cur.description
cur.close()
conn.close()
数据库形式如图
相关文章推荐
- Python开发简单爬虫(笔记)
- Python开发简单爬虫 - 慕课网
- Python 开发简单爬虫 - 实战演练
- Python开发简单爬虫
- Python开发简单爬虫(一)
- Python开发简单爬虫
- Python开发简单爬虫之爬虫介绍(一)
- Python开发简单爬虫
- python开发简单爬虫——笔记(更新中)
- python 开发简单爬虫
- Python开发简单爬虫学习笔记(2)
- ”Python开发简单爬虫“慕课网课程学习笔记1
- python开发简单爬虫:实战篇
- python-慕课网-Python开发简单爬虫-蚂蚁-读书笔记
- Python开发简单爬虫(一)
- Python 开发简单爬虫 学习笔记1
- Python开发简单爬虫--学习笔记
- 【Python开发】【神经网络与深度学习】如何利用Python写简单网络爬虫
- Python开发简单爬虫(二)---爬取百度百科页面数据
- Python开发简单爬虫