I always wanted to find time to learn python. This time I encountered a task of collecting web page information and finally I could give myself an excuse to learn it. At first, I thought it was very troublesome to re-learn a new language. Later, I confirmed that python is really very simple (of course, this is because you can set up the environment smoothly, find the corresponding module, and the Environment configuration is everywhere on the Internet. I will not talk about it here)
The following is a brief example to show you the advantages of Python:
ProgramTask: Collect thumbnails of videos on a video webpage and save them to a local file.
Environment: Eclipse + pydev
Version: python3.2.2 + beautifulsoup4
Import Urllib. Request, sys
Import Itertools, re
From Bs4Import Beautifulsoup
Ur = urllib. Request
Url_ I = 1
Pic_num = 1
# Conversion Function of custom quotation mark format
Def _ En_to_cn (STR ):
OBJ = itertools. cycle ([ ' " ' , ' " ' ])
_ OBJ =Lambda X: Next (OBJ)
Return Re. sub (R " ['\ "] " , _ OBJ, STR)
If _ Name __ = ' _ Main __ ' :
While Url_ I <= 30:
Webcontent = Ur. urlopen ( " Http://news.youku.com/focus/index/_page26716 _ " + STR (url_ I) + " . Html " )
Data = webcontent. Read ()
Soup = beautifulsoup (data)
Tag = soup. img
Print ( " ----------------------- Page " + STR (url_ I) + " ------------------------- " )
For Item In Soup. find_all (tag. Name ):
For ATTR In Item. attrs:
If ATTR = ' Title ' :
Ur. urlretrieve (item [ ' SRC ' ]," D: \ thumbnails \\ " + STR (pic_num) + " . " + _ En_to_cn (item [ ' Title ' ]) + " . Jpg " )
Pic_num + = 1
Print ( " ---- OK, page " + STR (url_ I) + " Has finished! " )
Url_ I + = 1
The program is very short. You can save the video thumbnails of any consecutive multiple pages on Youku. The key of the program is to use beautifulsoup, a third-party package used to parse the webpage. The official webpage of this package is:
Http://www.crummy.com/software/beautifulsoup/, which contains detailed documents for reference.
PS: in actual programming, we found that many video titles contain many quotation marks, some of which are in Chinese format and some are in English format. In Windows, quotation marks in English format are considered invalid characters in the file name. Therefore, a custom function _ en_to_cn is used to convert strings containing English quotation marks into Chinese quotation marks.
This example clearly shows the advantages of Python, especially in terms of Internet applications and natural language processing, this makes me more determined to learn python. Sharing ~