最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python爬蟲(chóng)實(shí)現(xiàn)使用beautifulSoup4爬取名言網(wǎng)功能案例

 更新時(shí)間:2019年09月15日 10:00:21   作者:_a_0_  
這篇文章主要介紹了Python爬蟲(chóng)實(shí)現(xiàn)使用beautifulSoup4爬取名言網(wǎng)功能,結(jié)合實(shí)例形式分析了Python基于beautifulSoup4模塊爬取名言網(wǎng)并存入MySQL數(shù)據(jù)庫(kù)相關(guān)操作技巧,需要的朋友可以參考下

本文實(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ì)有所幫助。

相關(guān)文章

最新評(píng)論

从江县| 东安县| 罗源县| 长治县| 青铜峡市| 长阳| 尤溪县| 常德市| 海盐县| 云浮市| 乌兰浩特市| 辽阳县| 内丘县| 南木林县| 揭西县| 清远市| 盐边县| 晋江市| 塘沽区| 江川县| 田东县| 进贤县| 洪洞县| 梅州市| 渭南市| 赣州市| 临武县| 太原市| 集贤县| 孙吴县| 进贤县| 阜阳市| 黄陵县| 溆浦县| 肥城市| 吴川市| 谷城县| 会泽县| 东平县| 潞城市| 三明市|