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

