How to use Python to gracefully complete database classes (construction ing)

Source: Internet
Author: User
Tags microsoft sql server management studio microsoft sql server management studio sql server management sql server management studio

0 Preface

Accidentally found that Google included in the school test clock system interface, just to do database lesson set, then take as environment.
The computer room incredibly loaded python, has long been told that Python crawler speed first-class, class, the DDL finished also idle, then decided to use Python to complete the database lesson set.

1 Crawler 1.1 HTTP Access

Crawler access to the Web page needs to import an HTTP access package, because the interface is too simple, the direct GET request can get data, even COOKIES are not used, directly with Urllib.

url="yoururladdress"req=urllib.request.Request(url)resp=urllib.request.urlopen(req)data=resp.read().decode('GBK')
1.2 Crawling content

The crawled Web page needs to bulk remove the intermediate text of two known strings, which can be easily resolved with regular expressions, with the import re package.

w1='<td width=\'610\' height=\'25\' style=\'padding-left:5px;\'>'w2='</td></tr>'pat=re.compile(w1+'(.*?)'+w2,re.S)sybz=pat.findall(data)

2 Database Operations

Tried several kinds of bags, and finally found that the PYODBC good
PYODBC Official Document: Https://github.com/mkleehammer/pyodbc/wiki

2.1 Connecting Microsoft SQL Server R2

First, enter the pip install pyodbc install PYODBC package on the command line
Open SQL Server Configuration Manager, select SQL Server Network configuration-MSSQLSERVER protocol-TCP/IP

Right-click the property to find the port that is set, typically 127.0.0.1:1433

After you open Microsoft SQL Server Management Studio, connect the local database on the right-click Properties, select Security, and then check the server authentication in the SQL Server and WINDOWS authentication Mode checkboxes.

Open the properties of the personal database on the left, select the file, assign the SA owner permission

Open Security-Login-SA, right-click Properties to specify a password for SA, and enforce password policy deselect

Select the status, and the settings are complete:

Create a new PY file and enter the following code (note change the YourPassword to your own password):

import pyodbccnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=127.0.0.1;DATABASE=SPDG;UID=sa;PWD=yourpassword')cursor = cnxn.cursor()

Simply execute a command to see the effect:

cursor.execute("SELECT * FROM SPB")row = cursor.fetchall()print(row)

How to use Python to gracefully complete database classes (construction ing)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.