pymysql

PyMySQL different updates in one query?

徘徊边缘 提交于 2019-12-10 01:11:51
问题 So I have a python script that goes through roughly 350,000 data objects, and depending on some tests, it needs to update a row which represents each one of those objects in a MySQl db. I'm also using pymysql as I've had the least trouble with it especially when sending over large select queries (select statements with where column IN (....) clause that can contain 100,000+ values). Since each update for each row can be different, each update statement is different. For example, for one row

ubuntu下django连接mysql报错详解

China☆狼群 提交于 2019-12-09 19:16:35
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module. Did you install mysqlclient? 首先安装pymysql在pycharm中点击 File Settings 在这里插入代码片 project Project Interpreter 点击右方加号,直接搜索 pymysql 然后左下角点击 Install Package 提示安装成功后关闭。 然后在与想settings.py 同一文件夹的_init_.py中增加 import pymysql pymysql.install_as_MySQLdb() 之后在报错 django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3. 说你的pymysql的版本是0.9.3, 同时需要mysqlclient是1.3.13。这个时候根据报错的内容去更改base.py文件。我的base.py文件目录在将下面这行 ` if version < ( 1, 3, 13 ) : raise ImproperlyConfigured ( 'mysqlclient 1.3.13 or newer is

python爬取静态数据并存入数据库

自作多情 提交于 2019-12-09 18:15:25
python爬取静态数据并存入数据库 连接mysql数据库 导入pymysql,连接数据库 在mysql中创建数据表 create table hw_info ( id varchar ( 20 ) primary key , music_num varchar ( 20 ) , person_num varchar ( 20 ) , person_name varchar ( 20 ) , feel varchar ( 20 ) , music_url varchar ( 100 ) ) import pymysql db = pymysql . connect ( 'ip' , 'QINYUYOU' , '密码' , 'homework' ) cursor = db . cursor ( ) print ( '连接成功' ) 爬取数据 我们爬取的网站为: 网站地址 需要爬取序号,音频编号,专家ID,专家名字,情感状态,音频地址。 f12查看headers 找到cookie和user-Agent,设置headers headers = { 'cookie' : 'PHPSESSID=c704263b13bb6f919e7534be3690a04f' , 'user-Agent' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537

python3简单爬虫并存入mysql数据库

◇◆丶佛笑我妖孽 提交于 2019-12-08 22:57:35
python3简单爬虫并存入mysql数据库 网络爬虫是一种高效的信息采集器,利用它可以快速、准确地采集我们想要的各种的数据资源。因此,可以说,网络爬虫技术几乎已成为大数据时代IT从业时代的必修课程。 爬取当当网商品数据(图片,价格,作者) (1)导入包 import requests from bs4 import BeautifulSoup beautifulsoup是python的一个HTML解析库,可以用它来方便地从网页中提取数据。 (2)发送请求并打印状态码 #添加请求头 修改user-agent来伪装浏览器 headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'} url = 'http://category.dangdang.com/cp01.19.34.00.00.00.html' res = requests.get(url,headers=headers) print(res.status_code) 添加headers伪装浏览器 (3)分析网页发现商品在<li>标签中 通过beautifulsoup查找到所有的商品

Python数据库连接池DBUtils

点点圈 提交于 2019-12-08 22:31:30
DBUtils是Python的一个用于实现数据库连接池的模块。 此连接池有两种连接模式: 模式一:为每个线程创建一个连接,线程即使调用了close方法,也不会关闭,只是把连接重新放到连接池,供自己线程再次使用。当线程终止时,连接自动关闭。    POOL = PersistentDB( creator=pymysql, # 使用链接数据库的模块 maxusage=None, # 一个链接最多被重复使用的次数,None表示无限制 setsession=[], # 开始会话前执行的命令列表。如:["set datestyle to ...", "set time zone ..."] ping=0, # ping MySQL服务端,检查是否服务可用。# 如:0 = None = never, 1 = default = whenever it is requested, 2 = when a cursor is created, 4 = when a query is executed, 7 = always closeable=False, # 如果为False时, conn.close() 实际上被忽略,供下次使用,再线程关闭时,才会自动关闭链接。如果为True时, conn.close()则关闭链接,那么再次调用pool.connection时就会报错,因为已经真的关闭了连接

Scrapy爬数据并存储到mysql中

你离开我真会死。 提交于 2019-12-08 22:12:23
前言 看这篇文嘉,假设你已经: 1. 安装了Srcapy框架 2. 安装了mysql 3. import了pymysql 后面两个可以参考我的前一篇博客,请移步: Python3.x连接Pymysql 首先来说说我踩的坑 坑1   我定义好items后,下一步是要将items这个容器引入到我的spider里面。所以要在spider文件中写上这么一句话: from scrapytest.CourseItems import CourseItem   scrapytest是我的工程名,CourseItem是我的item的类名。但是这句话在我的notebook里面死活给我报错: No Module named "scrapytest"    What? 我的scrapytest好好的在那啊。然后我建立一个同层的py文件,就是好的,所以我推测可能在notebook上面没有这种导入吧。 坑2   我最后爬的数据,根本就存储不到数据库中,所以我检查了几个步骤来排查原因: 1. 到底有没有爬到数据?   我写了一个生成json文件的类,然后去看了我保存下来的json,里面是有数据的,所以是数据库插入的问题。 2. 在连接“数据池“的时候出现了问题?   我在连接函数的内部打了log,结果可以打印log,所以连接数据库没问题。 3. 数据库启动了没?   我重新写了一个简单的插入

使用python爬取链家上海二手房信息的案例

孤者浪人 提交于 2019-12-08 21:16:44
使用python爬取链家上海二手房信息的案例 1、需求分析 爬取链家网上海的二手房信息,包括名称、户型、面积、价格信息,并将爬取到的信息写入到数据库中。 2、步骤分析 2.1 确定待爬取的url https://sh.lianjia.com/ershoufang/pg 在谷歌浏览器中查看网页源代码:ctrl+shif+i,先点击图中左上角框中的按钮,再在网页中点击需要查看的内容就会定位到对应的代码。 2.2确定爬取的数据 确定爬取的名称和户型的标签为 data-el=”region” 确定爬取的二手房的价格的标签class类名:class=”totalPrice” 3、代码实现 #从urllib中导入request,接受一个Request类的实例来设置URL请求的headers from urllib import request #导入正则表达式模块 import re #导入pymysql,将爬取到的数据写入mysql中 import pymysql def HTMLspider (url,startPage,endPage) : #作用:负责处理URL,分配每个URL去发送请求 for page in range(startPage,endPage+ 1 ): filename= "第" + str(page) + "页.html" #组合为完整的url fullurl

pymysql select in with variable number of parameters

时光总嘲笑我的痴心妄想 提交于 2019-12-08 12:20:39
问题 I read several examples that show how pymysql "select in" should work. So, this example works just fine: sql_select = 'SELECT a.user_id, AVG(a.rcount) AS \'average\' ' \ 'FROM (SELECT user_id, item_id, count(*) AS rcount ' \ 'FROM submission AS qsm ' \ 'JOIN metadata as qm ' \ 'ON qsm.item_id = qm.id ' \ 'WHERE qsm.item_id NOT IN (1, 2, 5, 6, 7, 147, 148) ' \ 'AND DATE(FROM_UNIXTIME(submission_time)) BETWEEN %s AND %s ' \ 'AND qm.type != \'survey\' ' \ 'GROUP BY user_id, item_id ' \ 'ORDER BY

PyMySQL using localhost vs socket incoherant behaviour

扶醉桌前 提交于 2019-12-08 10:56:03
问题 I am using PyMySQL to connect to a database running on localhost. I can access the database just fine using the username/password combiunation in both the command line and adminer so the database does not appear to be the probem here. My code is as follow. However, when using the host="127.0.0.1" options, I get an OperationalError and an Errno 111 . Using the same code, but connecting via the socket Mariadb runs on is fine. import pymysql.cursors from pprint import pprint # This causes an

pymysql connection select query and fetchall return tuple that has byte literals like b'25.00' rather than strings like '25.00'

怎甘沉沦 提交于 2019-12-08 10:15:40
问题 I have a python script that runs fine when only connecting to my local test machine having MySQL 5.6 database on Windows 8.1 , using pymysql connection. Select query / fetchal() returns tuples like ('1', '2015-01-02 23:11:19', '25.00'). However, when I use the same script slightly modified to include a second connection to a remote MySQL 5.0.96 production database running on a Linux server, it returns tuples like (b'1', b'2015-01-02 23:11:19', b'25.00') and the script does not run correctly