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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-03-20 我要评论
想了解使用python BeautifulSoup库抓取58手机维修信息的相关内容吗,在本文为您仔细讲解python BeautifulSoup库抓取网站信息的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:python,BeautifulSoup,下面大家一起来学习吧。

直接上代码:

复制代码 代码如下:

#!/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/

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们