本实战主要是为了进一步熟悉xpath的使用,再者就是将数据保存到Mysql数据库中,学习使用Mysql.
import pymysql
import requests
from lxml import etree
#创建数据库
def connectDB():
#在此处我的数据库密码password为空,如果用到此代码需要将password设置为自己的密码
db = pymysql.connect(host = 'localhost',user = 'root',password = None, port = 3306)
cursor = db.cursor()
cursor.execute("CREATE DATABASE newmovie DEFAULT CHARACTER SET utf8")
db.close()
#创建表
def createTable():
createTableSql = 'CREATE TABLE IF NOT EXISTS newmovies (title VARCHAR(255)NOT NULL ,src VARCHAR(255)NOT NULL ,score DECIMAL NOT NULL)'
db = pymysql.connect(host = 'localhost',user = 'root',password = None, port = 3306,db = 'newmovie')
cursor_create = db.cursor()
cursor_create.execute(createTableSql)
db.close()
print('create table' + 'newmovie' + 'successfully')
def crawl_html():