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

yipeiwu_com5年前Python爬虫

直接上代码:

复制代码 代码如下:

#!/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()+'\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爬虫 批量下载zabbix文档代码实例

这篇文章主要介绍了python爬虫 批量下载zabbix文档代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 # -*- c...

Python爬虫实现百度图片自动下载

Python爬虫实现百度图片自动下载

制作爬虫的步骤 制作一个爬虫一般分以下几个步骤: 分析需求分析网页源代码,配合开发者工具编写正则表达式或者XPath表达式正式编写 python 爬虫代码 效果预览 运行效果如下:...

利用python-pypcap抓取带VLAN标签的数据包方法

利用python-pypcap抓取带VLAN标签的数据包方法

1、背景介绍 在采用通常的socket抓包方式下,操作系统会自动将收到包的VLAN信息剥离,导致上层应用收到的包不会含有VLAN标签信息。而libpcap虽然是基于socket实现抓包,...

Python制作简单的网页爬虫

1.准备工作: 工欲善其事必先利其器,因此我们有必要在进行Coding前先配置一个适合我们自己的开发环境,我搭建的开发环境是: 操作系统:Ubuntu 14.04 LTS Pytho...

python requests爬取高德地图数据的实例

如下所示: 1.pip install requests 2.pip install lxml 3.pip install xlsxwriter import requests #想...