This article mainly introduces how Python uses bs4 to obtain 58 city classifications in the same city. It involves the skills of using the BeautifulSoup library to parse html pages in Python, for more information about how to use Python bs4 to obtain 58 city classifications, see the following example. Share it with you for your reference. The details are as follows:
# -*- coding:utf-8 -*-#! /usr/bin/pythonimport urllibimport os, datetime, sysfrom bs4 import BeautifulSoupreload(sys) sys.setdefaultencoding( "utf-8" ) __BASEURL__ = "http://bj.58.com/"__INITURL__ = "http://bj.58.com/hezu/"soup=BeautifulSoup(urllib.urlopen(__INITURL__))lv1Elements = soup.html.body.section.find('p', 'relative').find('dl', 'secitem')('a',href=True)f=open('data.txt', 'w')for element in lv1Elements[1:]: f.write((element.get_text() + '\r\n')) print element.get_text() url = __BASEURL__ + element.get('href') print url soup=BeautifulSoup(urllib.urlopen(url)) lv2Elements = soup.html.body.section.find('p', 'relative').find('dl', 'secitem').find('p', 'subarea').find_all('a') texts = [t.get_text() for t in lv2Elements] f.write(' '.join(texts) + '\r\n\r\n')f.close()
I hope this article will help you with Python programming.