使用python BeautifulSoup库抓取58手机维修信息

每个人的成功之路可能有所不同,但我相信成功需要每个想要成功的人人民努力奋斗和奋斗,通往成功的每条道路都是坎s曲折。

直接上代码:


#!/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',rel="nofollow noopener noreferrer" href=True)

f = open('data1.txt','a')

for element in lvlELements[1:]:

f.write((element.get_text()+'\n\r' ))

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('地址:'+addr +' 电话:'+ phone + '\r\n\r')

f.close()

直接执行后,存在 data1.txt中就会有商家的地址和电话等信息。
BeautifulSoup api 的地址为: http://www.crummy.com/software/BeautifulSoup/bs4/doc/

到此这篇关于使用python BeautifulSoup库抓取58手机维修信息就介绍到这了。因为有了梦想,才可以拥有奋斗的目标,而这些目标凝结成希望的萌芽,在汗水与泪水浇灌下,绽放成功之花。更多相关使用python BeautifulSoup库抓取58手机维修信息内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

标签: 手机维修 python