SoFunction
Updated on 2024-11-13

Python to realize the method of grabbing web pages to generate Excel file example

In this article, the example of Python to realize the method of grabbing web pages to generate Excel files. Shared for your reference, as follows:

Python grab web pages, the main use of PyQuery, this is the same as the jQuery usage, super power!

The sample code is as follows:

#-*- encoding:utf-8 -*-
import sys
import locale
import string
import traceback
import datetime
import urllib2
from pyquery import PyQuery as pq
# Determine the encoding of the runtime environment
reload(sys);
('utf8');
f = open('', 'w');
for i in range(1,24):
  d = pq(url="/?Code=HANGYELINGYU&myFlag=allShow&SiteID=122&PageIndex=%d"%(i));
  itemsa=d('dl dt a') # Fetch title element
  itemsb=d('dl dd') # Fetch title element
  for j in range(0,len(itemsa)):
    ("%s,\"%s\"\n"%(itemsa[j].get('title'),itemsb[j*2].text));
  #end for
#end for
();

The next step is to open it with Notepad++ and convert it to ANSI encoded format and save it. Then open this csv file with Excel software and save as Excel file

Readers interested in more Python related content can check out this site's topic: theSummary of Python Excel sheet manipulation techniques》、《Summary of Python file and directory manipulation techniques》、《Summary of Python text file manipulation techniques》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniquesand thePython introductory and advanced classic tutorials

I hope that what I have said in this article will help you in Python programming.