Python implements a simple crawler share that crawls links on a page

Source: Internet
Author: User
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://
$ python Install

Windows users click Download directly. Unzip and then locally use the command Python install. Https://

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 (' ')
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 (' '), 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.

  • 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: 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.