当前位置:首页 > Python教程 > python技巧

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·数据爬虫常见问题

原文:https://www.cnblogs.com/herotxl/p/15194954.html


【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!