python3爬取数据至mysql的方法


Posted in Python onJune 26, 2018

本文实例为大家分享了python3爬取数据至mysql的具体代码,供大家参考,具体内容如下

直接贴代码

#!/usr/local/bin/python3.5 
# -*- coding:UTF-8 -*- 
from urllib.request import urlopen 
from bs4 import BeautifulSoup 
import re 
import datetime 
import random 
import pymysql 
 
connect = pymysql.connect(host='192.168.10.142', unix_socket='/tmp/mysql.sock', user='root', passwd='1234', db='scraping', charset='utf8') 
cursor = connect.cursor() 
cursor.execute('USE scraping') 
 
random.seed(datetime.datetime.now()) 
 
 
def store(title, content): 
 
  execute = cursor.execute("select * from pages WHERE `title` = %s", title) 
  if execute <= 0: 
    cursor.execute("insert into pages(`title`, `content`) VALUES(%s, %s)", (title, content)) 
    cursor.connection.commit() 
  else: 
    print('This content is already exist.') 
 
 
def get_links(acticle_url): 
  html = urlopen('http://en.wikipedia.org' + acticle_url) 
  soup = BeautifulSoup(html, 'html.parser') 
  title = soup.h1.get_text() 
  content = soup.find('div', {'id': 'mw-content-text'}).find('p').get_text() 
  store(title, content) 
  return soup.find('div', {'id': 'bodyContent'}).findAll('a', href=re.compile("^(/wiki/)(.)*$")) 
 
links = get_links('') 
 
try: 
  while len(links) > 0: 
    newActicle = links[random.randint(0, len(links) - 1)].attrs['href'] 
    links = get_links(newActicle) 
    print(links) 
finally: 
  cursor.close() 
  connect.close()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
使用基于Python的Tornado框架的HTTP客户端的教程
Apr 24 Python
Python 通过pip安装Django详细介绍
Apr 28 Python
详解Python中的Numpy、SciPy、MatPlotLib安装与配置
Nov 17 Python
TensorFlow实现Softmax回归模型
Mar 09 Python
Python读写docx文件的方法
May 08 Python
PyQt5实现从主窗口打开子窗口的方法
Jun 19 Python
解决ROC曲线画出来只有一个点的问题
Feb 28 Python
使用python检查yaml配置文件是否符合要求
Apr 09 Python
Python中logger日志模块详解
Aug 04 Python
python 写一个文件分发小程序
Dec 05 Python
matplotlib绘制多子图共享鼠标光标的方法示例
Jan 08 Python
Python turtle实现贪吃蛇游戏
Jun 18 Python
python清除函数占用的内存方法
Jun 25 #Python
Python IDLE清空窗口的实例
Jun 25 #Python
Python设置在shell脚本中自动补全功能的方法
Jun 25 #Python
PyCharm代码整体缩进,反向缩进的方法
Jun 25 #Python
Python代码块批量添加Tab缩进的方法
Jun 25 #Python
对python中for、if、while的区别与比较方法
Jun 25 #Python
详解Django+Uwsgi+Nginx的生产环境部署
Jun 25 #Python
You might like
星际争霸任务指南——虫族
2020/03/04 星际争霸
php操作mysqli(示例代码)
2013/10/28 PHP
php简单生成一组与多组随机字符串的方法
2017/05/09 PHP
在Laravel中使用DataTables插件的方法
2018/05/29 PHP
Smarty模板语法详解
2019/07/20 PHP
thinkphp5框架前后端分离项目实现分页功能的方法分析
2019/10/08 PHP
laravel中Redis队列监听中断的分析
2020/09/14 PHP
JS Excel读取和写入操作(模板操作)实现代码
2010/04/11 Javascript
jQuery学习笔记之jQuery选择器的使用
2010/12/22 Javascript
利用jQuery实现可输入搜索文字的下拉框
2013/10/23 Javascript
JavaScript中的this关键字使用方法总结
2015/03/13 Javascript
jquery移动点击的项目到列表最顶端的方法
2015/06/24 Javascript
Vue 父子组件、组件间通信
2017/03/08 Javascript
JS中IP地址与整数相互转换的实现代码
2017/04/10 Javascript
JS删除数组里的某个元素方法
2018/02/03 Javascript
JavaScript实现学生在线做题计时器功能
2018/12/05 Javascript
浅谈vue.use()方法从源码到使用
2019/05/12 Javascript
vue 动态组件用法示例小结
2020/03/06 Javascript
基于vue实现探探滑动组件功能
2020/05/29 Javascript
vue+Element-ui实现登录注册表单
2020/11/17 Javascript
python基于mysql实现的简单队列以及跨进程锁实例详解
2014/07/07 Python
Python多线程编程(二):启动线程的两种方法
2015/04/05 Python
Python 模板引擎的注入问题分析
2017/01/01 Python
Python求两个圆的交点坐标或三个圆的交点坐标方法
2018/11/07 Python
Python3爬虫全国地址信息
2019/01/05 Python
pandas如何处理缺失值
2019/07/31 Python
python使用自定义钉钉机器人的示例代码
2020/06/24 Python
使用HTML5原生对话框元素并轻松创建模态框组件
2019/03/06 HTML / CSS
美国最好的钓鱼、狩猎和划船装备商店:Bass Pro Shops
2018/12/02 全球购物
伦敦鲜花递送:Flower Station
2021/02/03 全球购物
大学军训感言1000字
2014/02/25 职场文书
4s店销售经理岗位职责
2014/07/19 职场文书
我爱家乡演讲稿
2014/09/12 职场文书
学前班语言教学计划
2015/01/20 职场文书
大学生个人简历自荐信
2015/03/06 职场文书
Java存储没有重复元素的数组
2022/04/29 Java/Android