代码如下:

from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
import datetime
import random
import pymysql.cursors # Connect to the database
connection = pymysql.connect(host='127.0.0.1',
port=3306,
user='root',
password='数据库密码',
db='scraping',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor) cur = connection.cursor() random.seed(datetime.datetime.now()) def store(title,content):
cur.execute("INSERT INTO pages(title,content)values(\"%s\",\"%s\")",(title,content))
cur.connection.commit() def getLinks(articleUrl):
html = urlopen("http://en.wikipedia.org"+articleUrl)
bsObj = BeautifulSoup(html,"html.parser")
title = bsObj.find("h1").get_text()
print(title)
content = bsObj.find("div",{"id":"mw-content-text"}).find("p").get_text()
print(content)
store(title,content)
return bsObj.find("div",{"id":"bodyContent"}).findAll("a",href=re.compile("^(/wiki/)((?!:).)*$")) links = getLinks("/wiki/Kevin_Bacon") try:
while len(links) > 0 :
newArticle = links[random.randint(0, len(links)-1)].attrs["href"]
#print(newArticle)
links = getLinks(newArticle) finally:
cur.close()
connection.close()

结果截图

注:

  由于维基百科上我们会遇到各种各样的字符,所以最好通过下面四条语句让数据库支持unicode:

  

    alter database scraping character set = utf8mb4 collate = utf8mb4_unicode_ci;

    alter table pages  convert to character set = utf8mb4 collate = utf8mb4_unicode_ci;

    alter table pages change title title varchar(200) character set = utf8mb4 collate = utf8mb4_unicode_ci;

    alter table pages change content content varchar(10000) character set = utf8mb4 collate = utf8mb4_unicode_ci;

爬取维基百科人物介绍,并使用pymysql存储到数据库的更多相关文章

  1. 从0开始学爬虫8使用requests/pymysql和beautifulsoup4爬取维基百科词条链接并存入数据库

    从0开始学爬虫8使用requests和beautifulsoup4爬取维基百科词条链接并存入数据库 Python使用requests和beautifulsoup4爬取维基百科词条链接并存入数据库 参考 ...

  2. python+xpath+requests爬取维基百科历史上的今天

    import requests import urllib.parse import datetime from lxml import etree fhout = open("result ...

  3. 爬虫实战(一) 用Python爬取百度百科

    最近博主遇到这样一个需求:当用户输入一个词语时,返回这个词语的解释 我的第一个想法是做一个数据库,把常用的词语和词语的解释放到数据库里面,当用户查询时直接读取数据库结果 但是自己又没有心思做这样一个数 ...

  4. Python开发简单爬虫(二)---爬取百度百科页面数据

    一.开发爬虫的步骤 1.确定目标抓取策略: 打开目标页面,通过右键审查元素确定网页的url格式.数据格式.和网页编码形式. ①先看url的格式, F12观察一下链接的形式;② 再看目标文本信息的标签格 ...

  5. python简单爬虫 用beautifulsoup爬取百度百科词条

    目标:爬取“湖南大学”百科词条并处理数据 需要获取的数据: 源代码: <div class="basic-info cmn-clearfix"> <dl clas ...

  6. Python——爬取百度百科关键词1000个相关网页

    Python简单爬虫——爬取百度百科关键词1000个相关网页——标题和简介 网站爬虫由浅入深:慢慢来 分析: 链接的URL分析: 数据格式: 爬虫基本架构模型: 本爬虫架构: 源代码: # codin ...

  7. <爬虫>利用BeautifulSoup爬取百度百科虚拟人物资料存入Mysql数据库

    网页情况: 代码: import requests from requests.exceptions import RequestException from bs4 import Beautiful ...

  8. python爬虫—爬取百度百科数据

    爬虫框架:开发平台 centos6.7 根据慕课网爬虫教程编写代码 片区百度百科url,标题,内容 分为4个模块:html_downloader.py 下载器 html_outputer.py 爬取数 ...

  9. Python3爬取百度百科(配合PHP)

    用PHP写了一个网页,可以获取百度百科词条.源代码已分享至github:https://github.com/1049451037/xiaobaike/tree/master 那么通过Python来爬 ...

随机推荐

  1. JVM 性能排查--汇总

    参考:http://blog.sina.com.cn/s/blog_61d758500102wnus.html

  2. bzoj4302 Hdu 5301 Buildings

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4302 [题解] 出自2015多校-学军 题意大概是给出一个n*m的格子有一个格子(x,y)是 ...

  3. 标签 JLable 类

    标签JLable上可以添加图像,当鼠标停留在标签上时,可以显示一段提示文字. package first; import javax.swing.*; import java.awt.*; impor ...

  4. MySQL 进阶(待发布)

    视图 存储过程 触发器 基本函数

  5. cpu_relax( )-----对自选循环等待(spin-wait loops)操作的优化【转】

    cpu_relax()-----对自选循环等待(spin-wait loops)操作的优化 转自:http://www.doc100.net/bugs/t/173547/index.html 在loc ...

  6. PL/SQL 08 异常 exception

    --PL/SQL错误  编译时  运行时 --运行时的出错处理  EXCEPTION --异常处理块DECLARE …BEGIN …EXCEPTION WHEN OTHERS THEN  handle ...

  7. spring 声明式事务中try catch捕获异常

    原文:http://heroliuxun.iteye.com/blog/848122 今天遇到了一个这个问题 最近遇到这样的问题,使用spring时,在业务层需要捕获异常(特殊需要),当前一般情况下不 ...

  8. SpringMVC与Spring的父子容器关系

    问题: 在整合框架的时候有人也许会产生一个问题:能不能只配置一个扫描包加载实现类的扫描驱动,即在根目录下扫描所有的注解(@Controller.@Service.@Repository.@Compne ...

  9. 学习struts2

    有部分内容转载牛人的博客: http://blog.csdn.net/hudie1234567/article/details/6730481 http://blog.csdn.net/lishuan ...

  10. [ 总结 ] Linux下两种常用的双网卡绑定

    1. mode=0 (round-robin) 链路聚合:将两个或者更多数据信道结合成一个单一的信道,该信道以一个更高带宽的逻辑链路出现,链路聚合一般用来连接一个或多个带宽需求量大的设备,链路聚合是指 ...