First web crawler written using Python

Source: Internet
Author: User

Today try to use Python to write a web crawler code, mainly want to visit a website, select the information of interest, and save the information in a certain format in the early Excel.


This code mainly uses the following Python features, because Python is not familiar with, the code is also pasted below.


1. Open a Web page with a URL

Import Urllib2data = Urllib2.urlopen (String_full_link). Read (). Decode (' UTF8 ')
Print data

2, using regular expression matching

Import re# General English match reg = "" A href=\s* target= ' _blank ' title=\s* "" "Diclist = Re.compile (reg). FindAll (data) Print Diclist
#中文的正则匹配, you need to use unicode Codeaddrlist = Re.compile (reg) corresponding to the Unicode code reg=u "\u5730\u5740\s*"      # "Address" in Chinese. FindAll (sub_ Data
Print Addrlist

3. Write data to Excel file

Import xlrdimport xlwt        file = xlwt. Workbook ()        table = File.add_sheet (' HK ', cell_overwrite_ok=true)        Print index, name, addr, tel        table.write ( Index, 0, name) table.write (index,        1, addr)        table.write (Index, 2, tel)                file.save ("" "D:\\test.xls" "")


First web crawler written using Python

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