zoukankan      html  css  js  c++  java
  • 使用python抓取58手机维修信息

    之前在ququ的博客上看到说 python 中的BeautifulSoup 挺好玩的,今天下午果断下载下来,看了下api,挺好用的,完了2把,不错。

    晚上写了一个使用python抓取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()+'
    
    ' ))
    
        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 + '
    
    ')
    
    f.close()

    直接执行后,存在 data1.txt中就会有商家的地址和电话等信息。

    BeautifulSoup  api 的地址为: http://www.crummy.com/software/BeautifulSoup/bs4/doc/

    ---end---

  • 相关阅读:
    后缀自动机在复习
    冬令营模拟day2总结
    割点(模板)
    黑匣子
    挤牛奶
    上白泽慧音
    lca最小公共祖先祖先
    最小花费
    牛的旅行(标程)
    骑马修栅栏
  • 原文地址:https://www.cnblogs.com/yupeng/p/3348181.html
Copyright © 2011-2022 走看看