Python教程

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

本文主要是介绍Python之旅 3·数据爬虫常见问题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1·解决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

 

 

 

 

这篇关于Python之旅 3·数据爬虫常见问题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!