使用python BeautifulSoup库抓取58手机维修信息
直接上代码: 复制代码 代码如下: #!/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: f.close() 直接执行后,存在 data1.txt中就会有商家的地址和电话等信息。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |