编程语言
首页 > 编程语言> > Python之旅 3·数据爬虫常见问题

Python之旅 3·数据爬虫常见问题

作者:互联网

解决python爬虫requests.exceptions.SSLError: HTTPSConnectionPool(host='XXX', port=443)问题

方法:先检查pip有没安装cryptography,pyOpenSSL,certifi要是没有先安装
  pip install cryptography
  pip install pyOpenSSL
  pip install certifi

2·连接MySQL数据库问题

方法:

import pymysql
# 建立数据库连接
def getDBConnection():
    print('***************getDBConnection start')
    host = '127.0.0.1'
    port = 3306
    user = 'root'
    password = '123'
    db = 'music'
    charset = 'utf8'

    DBConnection = pymysql.connect(host=host, port=port, user=user, password=password, db=db, charset=charset)
    return DBConnection

# 保存音乐信息到数据库中
def saveMusicToDB(m_id, m_name, m_link, m_type, m_singer, m_album, m_click, m_collect):
    print('**********************saveMusicToDB start')
    DBConnection = getDBConnection()
    print('dbconnection=' + str(DBConnection))
    # 创建 游标
    cursor = DBConnection.cursor()
    sql = 'insert into orgmusic(m_id,m_name,m_link,m_type,m_singer,m_album,m_click,m_collect) values(%s,%s,%s,%s,%s,%s,%s,%s)'
    cursor.execute(sql, (m_id, m_name, m_link, m_type, m_singer, m_album, m_click, m_collect))

3·爬虫所需包

方法:

import urllib.request
from bs4 import BeautifulSoup
import pymysql
import datetime
import random
import gzip

 

 

 

 

标签:常见问题,DBConnection,Python,charset,爬虫,host,pip,import,port
来源: https://www.cnblogs.com/herotxl/p/15194954.html