语言:Python3.7
数据库:mysql
需要导入的工具包,如下:
from urllib.request import urlopen #注意这里的写法urllib不能直接写为import urllib要加上它的对象request
from bs4 import BeautifulSoup
import re
import time
import pymysql.cursors
import urllib.request
写入数据库需要创建表,代码如下:
CREATE TABLE imgtest(
id INT PRIMARY KEY AUTO_INCREMENT,
img LONGBLOB, -- 图片字段
content LONGTEXT -- 文本字段
);
#==============实现爬取图片到本地文件夹====================
url = "https://www.zhihu.com/question/22918070"
html = urllib.request.urlopen(url).read().decode('utf-8')
soup = BeautifulSoup(html,'html.parser')
print(soup.prettify())
#用Beautif