How Python extracts hyperlinks in Web pages

Source: Internet
Author: User
Many people begin to learn python at the beginning, and are going to use it as a crawler development. Since you want to be a crawler, you must first crawl the Web page and extract the hyperlink address from the Web page. This article for everyone to share a simple method, there is a need to refer to the reference.

Here is the simplest way to do this, first grab the target page back and then get the hyperlink by matching the href attribute in the a tag.

The code is as follows:

Import urllib2import Re url = ' http://www.sunbloger.com/' req = urllib2. Request (URL) con = urllib2.urlopen (req) doc = Con.read () con.close () links = Re.findall (R ' href\=\ "(http\:\/\/[a-za-z0-9\ . \/]+) \ "', doc) for a in Links:  print a


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.