Learn a little Python every day (2)

Source: Internet
Author: User

September 16 (python extension Installation and Use)

Continue with the previous article. According to the previous plan, analyze the exported data before further statistics.

The exported data is of the html type. The following processing methods are available:

1. Directly read lines and find the desired data in one row

2. Use the built-in HTMLParser to analyze HTML

3. Analyze HTML with Beautifulsoup

4. Analyze HTML with pyquery

5. convert HTML into Excel and use xlrd to analyze Excel

The first two methods are a bit difficult at present and tend to be the second three. Because the extension package needs to be installed in the following three methods, you should first learn to install the extension package.

The general method is to decompress the extension package and then run cmd to the directory of the extension package to execute python setup. py install (to execute python directly in cmd, you need to add the python directory to the environment variable path ).

The first method is to use easy_install. You may find ez_setup.py on the Internet. In cmd, python ez_setup.py is installed. You may also find the installation package on the Internet called setuptools. There is no problem. In fact, easy_install is only a command of setuptools, and the installation results are the same.

After you put the easy_install directory in Path, you can directly use the "easy_install extension package" to automatically download and update the desired extension package, the online saying is that he can solve the dependency problem between expansion packages by himself. But when I installed pyquery, I repeatedly reported an error. I looked at it carefully and found that the lxml package was missing. Search for lxml and download and install pyquery.

The extension package can be imported from... import to use, the first method is to import all, use the sys. argv is completely written in this way. The second type is to import a part. If only argv is imported, "sys. ". If it is written as" from sys import * ", you can omit" sys. "and import all, but it is too easy to confuse without adding the previous part.

September 17 (Beautifulsoup analysis HTML, SQLite getting started)

1. Use Beautifusoup

Take a closer look at Beautifusoup and pyquery. I prefer the Beautifulsoup style. I decided to use it!

Directly Add code

 bs4 === i  soup.findAll(                j=i.findAll('td')                tuple1=(j[2[3[4[5[6[7[8[9[11 StuList

First import BeautifulSoup,You must note that the writing method on the internet is changed from BeautifulSoup import BeautifulSoup to bs4,I discovered it in just half a day.

Define the method, open the HTML file, and use BeautifulSoup for parsing. This section describes two main methods: findAll and find. One is to find all, and the other is to find only the first one. The first two parameters of both methods are (Name = None, attrs = {}), Node name and attribute set. Traverse and put all the required values in a List.

To facilitate future use, save the code segment to getlist. py in the Python root directory. Next time, you can directly use import getlist. Note: Do not write it as import getlist. py.

2. SQLite getting started.

I am using windows X86. Here is.

Directly decompress the package and place it in a simple directory (you can also set the Path environment variable) for ease of use in cmd ). SQLite is really concise and I like it very much. Start with two commands.

sqlite3 woody.db sqlite> create table mytable(id integer primary key AUTOINCREMENT, name text); 

When a table is created, if the primary key does not increase automatically, the primary key will also increase automatically if it is not inserted, because in Sqlite, if the primary key is empty when a piece of data is inserted, he will find the largest primary key value in the existing table, and then + 1. However, this may cause the deleted primary key to continue .. Therefore, it is better to write AUTOINCREMENT.

Connect to the database using python

 =sqlite3.connect() sql=con.cousor() sql.execute() sql.fetchon()

Remember to write all the paths during the connection. Otherwise, a new database will be created directly under the python root directory. == #

You can use the following methods to connect a database to con.

commit()rollback()close()cursor()

Cursor SQL has the following methods.

execute()executemanyclose()fetchone()fetchmany()fetchall()scroll()

Parameterized insert operation

 t [(0,,),(1,,

 

September 21

In the twinkling of an eye, on the 22nd, we had a happy Mid-Autumn Festival. We had a lot of experience and thoughts during the three-day holiday. I wrote another article at night. I am tired of being dragged by projects. I haven't updated my learning progress for nearly a week.

.. Dragged by projects. stop learning python for a few days.

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.