Python爬蟲實(shí)現(xiàn)使用beautifulSoup4爬取名言網(wǎng)功能案例
本文實(shí)例講述了Python爬蟲實(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è)查詢的url #http://quotes.toscrape.com/tag/love/page/1/ #判斷到那一頁(yè)沒有數(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文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python爬取分析超級(jí)大樂(lè)透歷史開獎(jiǎng)數(shù)據(jù)
這篇文章主要介紹了python爬取分析超級(jí)大樂(lè)透歷史開獎(jiǎng)數(shù)據(jù),本次使用了requests和beautifulsoup庫(kù)進(jìn)行數(shù)據(jù)的爬取,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02詳解Python靜態(tài)網(wǎng)頁(yè)爬取獲取高清壁紙
這篇文章主要介紹了Python爬取高清壁紙,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04python實(shí)現(xiàn)簡(jiǎn)易五子棋游戲(控制臺(tái)版)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)易五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05python的列表生成式,生成器和generator對(duì)象你了解嗎
這篇文章主要為大家詳細(xì)介紹了python的列表生成式,生成器和generator對(duì)象,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03