[TOC] ## 抓取wiki ```python from urllib import request import re import pymysql from bs4 import BeautifulSoup sou = request.urlopen('https://zh.wikipedia.org/wiki/Main_page').read().decode('utf-8') soup = BeautifulSoup(sou, 'html.parser') #使用html.parser解析器 listUrls = soup.findAll('a', href=re.compile('^\/wiki\/')) for index, url in enumerate(listUrls, 1): # 23 : 手机 https://zh.wikipedia.org/wiki/%E6%89%8B%E6%9C%BA print(index, ":", url.string, "https://zh.wikipedia.org" + url['href']) connection = pymysql.connect(host='localhost', user='root', password='root', db='test', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) #插入 try: with connection.cursor() as cursor: # Create a new record sql = "INSERT INTO `urllist` (`urlname`, `urlhref`) VALUES (%s, %s)" string = (url.string, "暂无")[url.string is None] cursor.execute(sql, (string, "https://zh.wikipedia.org" + url['href'])) print(connection.commit()) except: print("all error") finally: connection.close() #查询 try: connection = pymysql.connect(host='localhost', user='root', password='root', db='test', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) with connection.cursor() as cursor: sql="SELECT `urlname`,`urlhref` from `urllist`" count = cursor.execute(sql) print(count) result = cursor.fetchmany(3) #取三条 print(result) except : print("has error") finally: connection.close() ```