The examples in this article describe how Python uses BS4 to get the 58 city classification of cities. Share to everyone for your reference. Specific 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 (' div ', ' 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 (' div ', ' relative '). Find (' DL ', ' Secitem '). Find (' div ', ' subarea '). Find_all ( ' A ') texts = [T.get_text () for T in Lv2elements] f.write (". Join (texts) + ' \r\n\r\n ') f.close ()
Hopefully this article will help you with Python programming.