代码如下:

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. HUSTOJ增加其他语言出现RuntimeError解决办法

    HUSTOJ增加其他语言,如Python.Java.Pascal等等,如果程序是正确的,却报运行错误,添加okcall就行. 具体错误可以看日志: [ERROR] A Not allowed syst ...

  2. 动态规划:树形DP

    典型例题有三道: 没有上司的舞会 选课 景点中心 我们可以把动态规划的状态和转移描述成DAG 对于有根树来说,如果我们规定边的方向由父节点指向叶子节点 或者是由叶子节点指向父节点(奇葩) 那么它也是一 ...

  3. WEB API 版本控制

    参照 http://blog.csdn.net/hengyunabc/article/details/20506345

  4. request.getParameterValues与request.getParameter的区别

    一. 简单的对比 request.getParameter用的比较多,相对熟悉 request.getParameterValues(String   name)是获得如checkbox类(名字相同, ...

  5. BZOJ 4823: [Cqoi2017]老C的方块

    分析: 我觉得我的网络流白学了...QAQ... 其实数据范围本是无法用网络流跑过去的,然而出题者想让他跑过去,也就跑过去了... 看到题目其实感觉很麻烦,不知道从哪里入手,那么仔细观察所给出的有用信 ...

  6. hdu 2141 Can you find it?(二分查找)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141 题目大意:查找是否又满足条件的x值. 这里简单介绍一个小算法,二分查找. /* x^2+6*x- ...

  7. shelve,xml,re模块

    一.shelve模块 shelve模块比pickle模块简单,只有一个open函数,返回类似字典的对象,可读可写;key必须为字符串,而值可以是python所支持的数据类型 import shelve ...

  8. pychart

    Pychart PyChart is a Python library for creating high quality Encapsulated Postscript, PDF, PNG, or ...

  9. POJ2479(最长连续子序列和)

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37035   Accepted: 11551 Des ...

  10. centos 自启动

    https://blog.phpha.com/backup/archives/1458.html 1.服务 chkconfig 服务名 on 查看所有可以 chkconfig --list 2 修改 ...