pymysql 安装
卸载原有的 pymysql,再执行安装 install pymysql
数据表
Py代码:
# 引入开发包 from urllib import request from bs4 import BeautifulSoup import re import pymysql.cursors req = request.Request('https://v.qq.com/channel/tv') req.add_header("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36") resp = request.urlopen(req).read() # 使用BeautifulSoup去解析 soup = BeautifulSoup(resp, 'html.parser') # 获取所有https://v.qq.com/x/cover/开头的a标签的href属性 listurl = soup.find_all("a", href=re.compile("^https://v.qq.com/x/cover/")) # 输出所有词条对应名称和url for url in listurl: # print(url.get_text(), '——>', "https://v.qq.com/channel/tv"+url['href']) # 获取数据库链接 connection = pymysql.connect(host='localhost', user='root', password='root', db='xsbike', charset='utf8mb4') try: # 获取回话指针 with connection.cursor() as cursor: # 创建sql语句 sql = "insert into `pyurl`(`urlname`, `urllink`) values(%s, %s)" # 执行sql语句 cursor.execute(sql, (url.get_text(), url['href'])) # 提交 connection.commit() finally: connection.close
文章评论(0)