使用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/

相关文章

selenium+python设置爬虫代理IP的方法

1. 背景 在使用selenium浏览器渲染技术,爬取网站信息时,一般来说,速度是很慢的。而且一般需要用到这种技术爬取的网站,反爬技术都比较厉害,对IP的访问频率应该有相当的限制。所以...

Python使用mongodb保存爬取豆瓣电影的数据过程解析

创建爬虫项目douban scrapy startproject douban 设置items.py文件,存储要保存的数据类型和字段名称 # -*- coding: utf-8...

Python使用Scrapy爬虫框架全站爬取图片并保存本地的实现代码

Python使用Scrapy爬虫框架全站爬取图片并保存本地的实现代码

大家可以在Github上clone全部源码。 Github:https://github.com/williamzxl/Scrapy_CrawlMeiziTu Scrapy官方文档:ht...

python3通过selenium爬虫获取到dj商品的实例代码

先给大家介绍下python3 selenium使用 其实这个就相当于模拟人的点击事件来连续的访问浏览器。如果你玩过王者荣耀的话在2016年一月份的版本里面就有一个bug。 安卓手机下载...

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

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