本文实例讲述了Python爬虫实现使用beautifulSoup4爬取名言网功能。分享给大家供大家参考,具体如下:

爬取名言网top10标签对应的名言,并存储到mysql中,字段(名言,作者,标签)

#! /usr/bin/python3

# -*- coding:utf-8 -*-

from urllib.request import urlopen as open

from bs4 import BeautifulSoup

import re

import pymysql

def find_top_ten(url):

response = open(url)

bs = BeautifulSoup(response,'html.parser')

tags = bs.select('span.tag-item a')

top_ten_href = [tag.get('href') for tag in tags]

top_ten_tag = [tag.text for tag in tags]

# print(top_ten_href)

# print(top_ten_tag)

return top_ten_href

def insert_into_mysql(records):

con = pymysql.connect(host='localhost',user='root',password='root',database='quotes',charset='utf8',port=3306)

cursor = con.cursor()

sql = "insert into quotes(content,author,tags) values(%s,%s,%s)"

for record in records:

cursor.execute(sql, record)

con.commit()

cursor.close()

con.close()

# http://quotes.toscrape.com/tag/love/

#要获取对应标签中所有的名言 所以这里要考虑分页的情况

#经过在网页上查看知道分页查询的url

#http://quotes.toscrape.com/tag/love/page/1/

#判断到那一页没有数据 div.container div.row [1]

def find_link_content(link):

page = 1

while True:

new_link = "http://quotes.toscrape.com" + link + "page/"

# print(new_link)

new_link = new_link + str(page)

print(new_link)

sub_bs = open(new_link)

sub_bs = BeautifulSoup(sub_bs,'html.parser')

quotes = sub_bs.select('div.row div.col-md-8 span.text')

# 如果没有数据就退出

if len(quotes) == 0:

break

#名言

quotes = [quote.text.strip('“”') for quote in quotes]

#作者

authors = sub_bs.select('small.author')

authors = [author.text for author in authors]

# 标签

tags_list = sub_bs.select('meta.keywords')

tags_list = [tags.get('content') for tags in tags_list]

# print(authors)

# print(quotes)

#print(tags_list)

record_list = []

for i in range(len(quotes)):

tags = tags_list[i]

tags = tags.replace(',',',')

print(tags)

record = [quotes[i],authors[i],tags]

record_list.append(record)

insert_into_mysql(record_list)

page += 1

#

def main():

url = "http://quotes.toscrape.com/"

parent_link = find_top_ten(url)

for link in parent_link:

print(link)

find_link_content(link)

if __name__ == '__main__':

main()

希望本文所述对大家Python程序设计有所帮助。

python功能性爬虫案例_Python爬虫实现使用beautifulSoup4爬取名言网功能案例相关推荐

  1. Python爬虫学习---------使用beautifulSoup4爬取名言网

    爬取名言网top10标签对应的名言,并存储到mysql中,字段(名言,作者,标签) #! /usr/bin/python3 # -*- coding:utf-8 -*-from urllib.requ ...

  2. Python爬虫学习笔记 (9) [初级] 小练习 爬取慕课网课程清单

    更新日期: 2021.03.28 本节学习内容 : 练习使用 bs4 和 xlwings - 爬取慕课网免费课程清单并存为 Excel 文件. 目录 1. 目标信息 2. 爬取步骤 3. 代码 5. ...

  3. python 批量下载网页图片_手把手教你爬取天堂网1920*1080大图片(批量下载)——实战篇|python基础教程|python入门|python教程...

    https://www.xin3721.com/eschool/pythonxin3721/ /1 前言/ 上篇文章 手把手教你爬取天堂网1920*1080大图片(批量下载)--理论篇我们谈及了天堂网 ...

  4. python交通调查数据处理_Python突破高德API限制爬取交通态势数据+GIS可视化(超详细)...

    一.需求: 爬取高德的交通态势API,将数据可视化为含有交通态势信息的矢量路网数据. 二.使用的工具: Python IDLE.记事本编辑器.ArcGIS 10.2.申请的高德开发者KEY(免费). ...

  5. 基于python的音乐数据分析_Python对QQ音乐进行爬取并进行数据分析

    三方包引入 使用到了以下包: 爬虫 scrapy 网络测试 requests 数据分析 numpy和pandas 绘图 matplotlib和wordcloud pip install scrapy ...

  6. python扇贝单词书_Python脚本 扇贝单词书爬取

    这是一个·用于爬取扇贝单词书的脚本 将在.py文件目录得到一个名为out.txt的输出文件 主要使用了selenium库(webdriver) 使用方式: 更改 13行中指向webdriver驱动器 ...

  7. 爬虫实战1-多进程爬取 名言网

    import requests import re from multiprocessing import Pooldef get_html(url, header=''):''':param url ...

  8. python卡路里程序_SpiderFlow平台v0.3.0初次使用并爬取薄荷网的热量和减法功效

    spider-flow 作为web爬虫他可以简单的说是新一代的爬虫平台,以图形化方式定义爬虫流程,不写代码即可完成爬虫. 也就是说我们不用在刻意的为了一些数据就去学一下语言如python,我们只要画个 ...

  9. python网页爬虫漫画案例_Python爬虫-用Scrapy框架实现漫画的爬取

    14.jpg 在之前一篇抓取漫画图片的文章里,通过实现一个简单的Python程序,遍历所有漫画的url,对请求所返回的html源码进行正则表达式分析,来提取到需要的数据. 本篇文章,通过 scrapy ...

最新文章

  1. Apache访问日志切割
  2. 人工智能 | SLAM与Visual Odometry技术综述(浙江大学智能系统和控制研究所)
  3. memcached安装运行
  4. Java中怎么样检查一个字符串是不是数字呢
  5. bootstrap实现表格
  6. 【转】Android 中的 Service 全面总结
  7. Alfresco安装与配置图解
  8. 【人工智能】NIPS2019 | 2019NIPS论文 | NeurIPS2019最新更新论文~持续更新| NIPS2019百度云下载
  9. C语言复变函数PPT,复变函数的孤立点及其应用.doc
  10. 搭建docker监控平台
  11. 超级记事本android,基于Android的超级记事本APP设计.doc
  12. 浙江计算机软件考试官网,浙江:全国计算机软件考试开始报名 11月8日开考
  13. 5G基站耗电剑指4G基站水平,通过IMT-2020(5G)大会看5G发展趋势
  14. EXCEL的COUNTIF和COUNTIFS函数的区别和联系
  15. 解决 Minecraft 官方启动器 报错找不到 launcher.dll / LoadErrorNotPresent 的解决方法
  16. 33、网络地址转换(NAT)
  17. 如何把flv转换成mp3格式 音频转换器哪个好
  18. 4D毫米波雷达天线设计
  19. chrome浏览器必备5款插件--各个都好使--chrome基础
  20. GridView分页实现

热门文章

  1. 深大操作系统实验四:磁盘 Inode 读取,盘块读取,软硬链接,磁盘挂载
  2. Attention-FH 通过深度强化学习的注意感知面孔幻觉 (读书笔记)
  3. 爬取南京链家租房信息
  4. soar SQL分析工具与soar-web可视化
  5. 战斗服务器响应超时 是否尝试重连,刺激战场:沙滩边有个隐藏技巧,只需3步,让你的船开往地下!...
  6. 想进阿里的悲剧了,马云说了今年不会增加一个员工(转载自钛媒体http://www.tmtpost.com/229500.html)
  7. python+django的插画师作品分享投稿网站
  8. css3教程3-边框圆角:四叶草
  9. 二十、接口的应用示例(便于理解使用场景)
  10. 理财 thread.php,理财牢记三句话 人人都能成为百万富翁(转载)