2
1
from urllib.request import urlopen
from bs4 import BeautifulSoup as bs
import pymysql.cursors
# 打开链接并读取,把结果用utf-8编码
resp = urlopen("http://www.umei.cc/bizhitupian/meinvbizhi/").read().decode("utf-8")
# 使用html.parser解析器
soup = bs(resp,"html.parser")
# 格式化输出
#print(soup.prettify())
#print(soup.img) # 获取img标签
#print(soup.find_all('img')) # 获取所有img标签信息
for link in soup.find_all('img'): # 从文档中找到所有img标签的链接
#print(link.get('src'))
#print(link.get('title'))
# 获取数据库连接
connection = pymysql.connect(host="localhost", user="root", password="root", db="python_mysql", charset="utf8mb4")
try:
#获取会话指针
with connection.cursor() as cursor:
#创建sql语句
sql = "insert into `girl_image`(`title`, `urlhref`) values (%s, %s)"
# 执行sql语句
cursor.execute(sql, (str(link.get('title')), link.get('src')))
#提交
connection.commit()
finally:
connection.close()
Python操作mysql步骤3
Python操作mysql使用步骤2
Python操作mysql使用步骤
存储数据到MySQL
查询数据mysql
#!/usr/bin/env python
# encoding: utf-8
#引入开发包
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
import pymysql
resp = urlopen("https://en.wikipedia.org/wiki/Main_Page").read().decode("utf-8")
soup = BeautifulSoup(resp, "html.parser")
listUrls = soup.find_all("a", href=re.compile("^/wiki/"))
#print(listUrls)
connection = pymysql.connect(host='localhost',
user='root',
password='',
db='wiki',
charset='utf8')
print(connection)
try:
with connection.cursor() as cursor:
for url in listUrls:
if not re.search("\.(jpg|jpeg)$", url['href']):
sql = "insert into `urls`(`urlname`,`urlhref`)values(%s, %s)"
#print(sql)
#print(url.get_text())
cursor.execute(sql, (url.get_text(), "https://en.wikipedia.org" + url["href"]))
connection.commit()
finally:
connection.close();