Python爬蟲實(shí)現(xiàn)使用beautifulSoup4爬取名言網(wǎng)功能案例
本文實(shí)例講述了Python爬蟲實(shí)現(xiàn)使用beautifulSoup4爬取名言網(wǎng)功能。分享給大家供大家參考,具體如下:
爬取名言網(wǎng)top10標(biāo)簽對應(yīng)的名言,并存儲到mysql中,字段(名言,作者,標(biāo)簽)
#! /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/
#要獲取對應(yīng)標(biāo)簽中所有的名言 所以這里要考慮分頁的情況
#經(jīng)過在網(wǎng)頁上查看知道分頁查詢的url
#http://quotes.toscrape.com/tag/love/page/1/
#判斷到那一頁沒有數(shù)據(jù) 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')
# 如果沒有數(shù)據(jù)就退出
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]
# 標(biāo)簽
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()
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python正則表達(dá)式用法總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python常用列表數(shù)據(jù)結(jié)構(gòu)小結(jié)
這篇文章主要介紹了Python常用列表數(shù)據(jù)結(jié)構(gòu)小結(jié),很有參考借鑒價值,需要的朋友可以參考下2014-08-08
Python實(shí)現(xiàn)Pig Latin小游戲?qū)嵗a
這篇文章主要介紹了Python實(shí)現(xiàn)Pig Latin小游戲?qū)嵗a,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02
python利用Excel讀取和存儲測試數(shù)據(jù)完成接口自動化教程
這篇文章主要介紹了python利用Excel讀取和存儲測試數(shù)據(jù)完成接口自動化教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Python實(shí)現(xiàn)圖像隨機(jī)添加椒鹽噪聲和高斯噪聲
圖像噪聲是指存在于圖像數(shù)據(jù)中的不必要的或多余的干擾信息。在噪聲的概念中,通常采用信噪比(Signal-Noise?Rate,?SNR)衡量圖像噪聲。本文將利用Python實(shí)現(xiàn)對圖像隨機(jī)添加椒鹽噪聲和高斯噪聲,感興趣的可以了解一下2022-09-09
Python深度學(xué)習(xí)實(shí)戰(zhàn)PyQt5窗口切換的堆疊布局示例詳解
本文以堆疊窗口控件為例,詳細(xì)介紹堆疊布局的界面設(shè)計(jì)和程序?qū)崿F(xiàn)過程,通過案例帶小白創(chuàng)建一個典型的堆疊布局多窗口切換程序2021-10-10
Python+Empyrical實(shí)現(xiàn)計(jì)算風(fēng)險指標(biāo)
Empyrical 是一個知名的金融風(fēng)險指標(biāo)庫。它能夠用于計(jì)算年平均回報(bào)、最大回撤、Alpha值等。下面就教你如何使用 Empyrical 這個風(fēng)險指標(biāo)計(jì)算神器2022-05-05
tensorflow 只恢復(fù)部分模型參數(shù)的實(shí)例
今天小編就為大家分享一篇tensorflow 只恢復(fù)部分模型參數(shù)的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01

