In addition to C + +, I have also been exposed to a lot of popular languages, PHP, Java, JavaScript, Python, which Python can be said to be the most convenient to operate, the least of the shortcomings of the language.
A few days ago I wanted to write a crawler, and then discussed with friends, decided to write together a few days later. The important part of the crawler is crawling the links on the page, and I'll simply implement it here.
First we need to use an open source module, requests. This is not a Python-brought module that needs to be downloaded, unzipped and installed from the Web:
Copy the Code code as follows:
$ Curl-ol Https://github.com/kennethreitz/requests/zipball/master
$ python setup.py Install
Windows users click Download directly. Unzip and then locally use the command Python setup.py install. Https://github.com/kennethreitz/requests/zipball/master
This module of the document I am also slowly translated, translated after the spread to everyone (English version first in the attachment). As its description says, built for human beings, designed for humans. It is convenient to use it and look at the documentation yourself. The simplest, requests.get () is to send a GET request.
The code is as follows:
Copy the Code code as follows:
# Coding:utf-8
Import re
Import requests
# Get Web Content
r = Requests.get (' http://www.163.com ')
data = R.text
# use regular to find all connections
Link_list =re.findall (r "<=href=\"). +? =\")| (? <=href=\ '). +? (? =\ ') ", data)
For URL in link_list:
Print URL
First import into re and requests module, RE module is the use of regular expression module.
data = Requests.get (' http://www.163.com '), submit a GET request to NetEase home page, get a Requests object R,r.text is the source code of the webpage obtained, save in the string data.
Then use the regular to find all the links in data, my regular writing is relatively coarse, directly to the href= "" or href= "between the information obtained, this is the link information we want.
Re.findall returns a list that iterates through the list with a For loop and outputs:
This is part of all the connections I get.
The above is a simple implementation to get all the links in the site, without handling any exceptions, regardless of the type of hyperlink, the code is for reference only. The Requests module documentation is contained in the annex.