Python provides an example of how to capture a web page to generate an Excel file,

Source: Internet
Author: User

Python provides an example of how to capture a web page to generate an Excel file,

This example describes how to capture a web page and generate an Excel file using Python. We will share this with you for your reference. The details are as follows:

Python mainly uses PyQuery to capture web pages. This is the same as jQuery's usage and is extremely powerful.

The sample code is as follows:

#-*-Encoding: UTF-8-*-import sysimport localeimport stringimport tracebackimport datetimeimport urllib2from pyquery import PyQuery as pq # determine the encodingreload (sys); sys. setdefaultencoding ('utf8'); f = open('gongsi.csv ', 'w'); for I in range (): d = pq (url = "http://www.yourwebname.com /? Code = HANGYELINGYU & myFlag = allShow & SiteID = 122 & PageIndex = % d "% (I); itemsa = d ('dl dt ') # title element itemsb = d ('dl dd') # title element for j in range (0, len (itemsa): f. write ("% s, \" % s \ "\ n" % (itemsa [j]. get ('title'), itemsb [j * 2]. text); # end for # end forf. close ();

Next, use notepad##to open gongsi.csv, convert it to the ANSI encoding format, and save it. Open the csv file in Excel and save it as an Excel file.

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.