Using the python BeautifulSoup library to capture 58 phone repair information

  • 2020-04-02 13:16:25
  • OfStack

Direct code:


#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib
import os,datetime,string
import sys
from bs4 import BeautifulSoup
reload(sys)
sys.setdefaultencoding('utf-8')
__BASEURL__ = 'http://bj.58.com/'
__INITURL__ = "http://bj.58.com/shoujiweixiu/"
soup = BeautifulSoup(urllib.urlopen(__INITURL__))
lvlELements = soup.html.body.find('div','selectbarTable').find('tr').find_next_sibling('tr')('a',href=True)
f = open('data1.txt','a')
for element in lvlELements[1:]:
    f.write((element.get_text()+'nr' ))
    url = __BASEURL__ + element.get('href')
    print url
    soup = BeautifulSoup(urllib.urlopen(url))
    lv2ELements = soup.html.body.find('table','tblist').find_all('tr')
    for item in lv2ELements:
        addr = item.find('td','t').find('a').get_text()
        phone = item.find('td','tdl').find('b','tele').get_text()
        f.write(' Address: '+addr +'  The phone :'+ phone + 'rnr')
f.close()

After direct execution, there will be the address and phone number of the business in data1.txt.
BeautifulSoup   API's address is: http://www.crummy.com/software/BeautifulSoup/bs4/doc/


Related articles: