代码如下:

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. Nginx替换过滤文本模块replace-filter-nginx-module

    1.安装此模块需要先安装sregex运行库 apt-get update;apt-get install git make gcc -y #Centos改成yum git clone https:// ...

  2. GoogleMap在js中的应用

    <html> <head> <meta name="viewport" content="initial-scale=1.0, user-s ...

  3. Linux下程序对拍_C++ (付费编号1001)

    本博客为精品博客,涉及利益问题,严禁转载,违者追究法律责任 一.对拍背景 对拍是一种十分实用的检查程序正确性的手段,在比赛时广泛使用 我们一般对拍两个程序,一个是自己不确定正确性的高级算法,另一个一般 ...

  4. ubuntu设置默认python版本

    原文:https://www.cnblogs.com/johnny1024/p/8400511.html ··· ubuntu 16.04本身是自带python的,他本身是自带2.X和3.X,两个版本 ...

  5. Codeforces 270E Flawed Flow 网络流问题

    题意:给出一些边,给出边的容量.让你为所有边确定一个方向使得流量最大. 题目不用求最大流, 而是求每条边的流向,这题是考察网络流的基本规律. 若某图有最大,则有与源点相连的边必然都是流出的,与汇点相连 ...

  6. React 踩坑记录

    1.React-router error: super expression must either be null or a function 原因:引入babel后写ES6风格的代码: class ...

  7. UT技巧

    (一)PowerMockito进行UT测试如何略过方法,使方法不被执行(含私有方法): PowerMockito.doNothing().when(TestMock.class,"foo1& ...

  8. shell字符串变量的特异功能:字符串的替换(${str/源模式/目标模式},${str//源模式/目标模式})、截断

    https://blog.csdn.net/wzb56_earl/article/details/6953612

  9. iPhone手机关闭ios10自动更新

    在手机上打开safari,地址栏输入:https://oldcat.me/web/NOOTA9.mobileconfig然后回车按照要求安装此provision文件即可,然后重启.桌面出现一个反馈感叹 ...

  10. [win7] 带网络的安全模式,启动QQEIMPlatform第三方服务

    REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\QQEIMPlatform" /VE /T REG_ ...