Python captures the url instance code of the Jingdong Mall mobile phone list

Source: Internet
Author: User

Copy codeThe Code is as follows:
#-*-Coding: UTF-8 -*-
'''
Created on 2013-12-5

@ Author: good-temper
'''

Import urllib2
Import bs4
Import time

Def getPage (urlStr ):
'''
Get Page Content
'''
Content = urllib2.urlopen (urlStr). read ()
Return content

Def getNextPageUrl (currPageNum ):
# Http://list.jd.com/9987-653-655-0-0-0-0-0-0-0-1-1-page code -1-1-72-4137-33.html
Url = u 'HTTP: // list.jd.com/9987-653-655-0-0-0-0-0-0-0-1-'{str (currpagenum=1#'-1-72-4137-33.html'

# Whether the next page exists
Content = getPage (url );
Soup = bs4.BeautifulSoup (content)
List = soup. findAll ('span ', {'class': 'next-disabled '});
If (len (list) = 0 ):
Return url
Return''

Def analyzeList ():
PageNum = 0
List = []
Url = getNextPageUrl (pageNum)
While url! = '':
Soup = bs4.BeautifulSoup (getPage (url ))
Pagelist = soup. findAll ('div ', {'class': 'P-name '})
For elem in pagelist:
Soup1 = bs4.BeautifulSoup (str (elem ))
List. append (soup1.find ('A') ['href '])

PageNum = pageNum + 1
Print pageNum
Url = getNextPageUrl (pageNum)
Return list

Def analyzeContent (url ):

Return''

Def writeToFile (list, path ):
F = open (path, 'A ')
For elem in list:
F. write (elem + '\ n ')
F. close ()

If _ name _ = '_ main __':
List = analyzeList ()
Print 'capture all '+ str (len (list) + 'entries \ N'

WriteToFile (list, u'e: \ jd_phone_list.dat ');

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.