Python爬蟲實現(xiàn)使用beautifulSoup4爬取名言網(wǎng)功能案例
本文實例講述了Python爬蟲實現(xiàn)使用beautifulSoup4爬取名言網(wǎng)功能。分享給大家供大家參考,具體如下:
爬取名言網(wǎng)top10標簽對應(yīng)的名言,并存儲到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/
#要獲取對應(yīng)標簽中所有的名言 所以這里要考慮分頁的情況
#經(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]
# 標簽
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正則表達式用法總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
Python常用列表數(shù)據(jù)結(jié)構(gòu)小結(jié)
這篇文章主要介紹了Python常用列表數(shù)據(jù)結(jié)構(gòu)小結(jié),很有參考借鑒價值,需要的朋友可以參考下2014-08-08
Python實現(xiàn)Pig Latin小游戲?qū)嵗a
這篇文章主要介紹了Python實現(xiàn)Pig Latin小游戲?qū)嵗a,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02
python利用Excel讀取和存儲測試數(shù)據(jù)完成接口自動化教程
這篇文章主要介紹了python利用Excel讀取和存儲測試數(shù)據(jù)完成接口自動化教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Python深度學習實戰(zhàn)PyQt5窗口切換的堆疊布局示例詳解
本文以堆疊窗口控件為例,詳細介紹堆疊布局的界面設(shè)計和程序?qū)崿F(xiàn)過程,通過案例帶小白創(chuàng)建一個典型的堆疊布局多窗口切換程序2021-10-10
Python+Empyrical實現(xiàn)計算風險指標
Empyrical 是一個知名的金融風險指標庫。它能夠用于計算年平均回報、最大回撤、Alpha值等。下面就教你如何使用 Empyrical 這個風險指標計算神器2022-05-05

