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)