本文实例讲述了Python爬虫实现使用beautifulSoup4爬取名言网功能。分享给大家供大家参考,具体如下:
爬取名言网top10标签对应的名言,并存储到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