您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

5b51 2022/1/14 8:17:37 python 字数 1947 阅读 331 来源 www.jb51.cc/python

直接上代码: 复制代码代码如下:#!/usr/bin/python#-*-coding:utf-8-*- importurllib importos,datetime,string

概述

直接上代码

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 BeautifulSoup库抓取58手机维修信息全部内容,希望文章能够帮你解决使用python BeautifulSoup库抓取58手机维修信息所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶