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 相关文章推荐
pycharm 使用心得(六)进行简单的数据库管理
Jun 06 Python
Windows8下安装Python的BeautifulSoup
Jan 22 Python
修改Python的pyxmpp2中的主循环使其提高性能
Apr 24 Python
python实现域名系统(DNS)正向查询的方法
Apr 19 Python
Python列表推导式、字典推导式与集合推导式用法实例分析
Feb 07 Python
Python使用pyautogui模块实现自动化鼠标和键盘操作示例
Sep 04 Python
解决pandas .to_excel不覆盖已有sheet的问题
Dec 10 Python
pycharm创建一个python包方法图解
Apr 10 Python
tensorflow 分类损失函数使用小记
Feb 18 Python
Python flask框架实现浏览器点击自定义跳转页面
Jun 04 Python
python爬虫 requests-html的使用
Nov 30 Python
Python如何用re模块实现简易tokenizer
May 02 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
PHP框架Swoole定时器Timer特性分析
2014/08/19 PHP
PHP生成短网址方法汇总
2016/07/12 PHP
JS取得绝对路径的实现代码
2015/01/16 Javascript
JavaScript 动态加载脚本和样式的方法
2015/04/13 Javascript
jQuery+PHP实现可编辑表格字段内容并实时保存
2015/10/09 Javascript
jQuery的Ajax用户认证和注册技术实例教程(附demo源码)
2015/12/08 Javascript
jquery实现全屏滚动
2015/12/28 Javascript
jQuery+css实现炫目的动态块漂移效果
2016/01/28 Javascript
layer实现弹窗提交信息
2016/12/12 Javascript
简易Vue评论框架的实现(父组件的实现)
2018/01/08 Javascript
用图片替换checkbox原始样式并实现同样的功能
2018/11/15 Javascript
实例讲解JavaScript截取字符串
2018/11/30 Javascript
深入浅析Vue中mixin和extend的区别和使用场景
2019/08/01 Javascript
Python中列表和元组的使用方法和区别详解
2020/12/30 Python
使用python生成目录树
2018/03/29 Python
神经网络(BP)算法Python实现及应用
2018/04/16 Python
Python读取excel中的图片完美解决方法
2018/07/27 Python
在PyCharm中批量查找及替换的方法
2019/01/20 Python
详解Python给照片换底色(蓝底换红底)
2019/03/22 Python
python+selenium实现自动化百度搜索关键词
2019/06/03 Python
使用python的pyplot绘制函数实例
2020/02/13 Python
python使用Windows的wmic命令监控文件运行状况,如有异常发送邮件报警
2021/01/30 Python
H5最强接口之canvas实现动态图形功能
2019/05/31 HTML / CSS
美国知名的女性服饰品牌:LOFT(洛芙特)
2016/08/05 全球购物
英国儿童图书网站:Scholastic
2017/03/26 全球购物
苹果音乐订阅:Apple Music
2018/08/02 全球购物
什么是Assembly(程序集)
2014/09/14 面试题
个人找工作求职简历的自我评价
2013/10/20 职场文书
自我鉴定200字
2013/10/28 职场文书
大学三年的自我评价
2013/12/25 职场文书
幼儿园儿童节活动主持词+串词大全
2014/03/21 职场文书
大二学习计划书范文
2014/04/27 职场文书
2014年国庆节演讲稿
2014/09/19 职场文书
外贸英文求职信范文
2015/03/19 职场文书
【HBU】数据库第四周 单表查询
2021/04/05 SQL Server
Spring Boot实现文件上传下载
2022/08/14 Java/Android