• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python使用bs4获取58同城城市分类的方法

Python使用bs4获取58同城城市分类的方法

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python bs4,python bs4下载,python bs4 安装,python3 bs4,python怎么安装bs4等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了Python使用bs4获取58同城城市分类的方法。分享给大家供大家参考。具体如下:

# -*- coding:utf-8 -*-
#! /usr/bin/python
import urllib
import os, datetime, sys
from bs4 import BeautifulSoup
reload(sys) 
sys.setdefaultencoding( "utf-8" ) 
__BASEURL__ = "http://bj.58.com/"
__INITURL__ = "http://bj.58.com/hezu/"
soup=BeautifulSoup(urllib.urlopen(__INITURL__))
lv1Elements = soup.html.body.section.find('div', 'relative').find('dl', 'secitem')('a',href=True)
f=open('data.txt', 'w')
for element in lv1Elements[1:]:
  f.write((element.get_text() + '\r\n'))
  print element.get_text()
  url = __BASEURL__ + element.get('href')
  print url
  soup=BeautifulSoup(urllib.urlopen(url))
  lv2Elements = soup.html.body.section.find('div', 'relative').find('dl', 'secitem').find('div', 'subarea').find_all('a')
  texts = [t.get_text() for t in lv2Elements]
  f.write(' '.join(texts) + '\r\n\r\n')
f.close()

</div>

希望本文所述对大家的Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • Python使用bs4获取58同城城市分类的方法

相关文章

  • python多线程编程中的join函数使用心得
  • Python编程中用close()方法关闭文件的教程
  • python插入数据到列表的方法
  • 用python + hadoop streaming 分布式编程(一) -- 原理介绍,样例程序与本地调试
  • 用Python实现服务器中只重载被修改的进程的方法
  • 使用Python判断IP地址合法性的方法实例
  • Python中Django 后台自定义表单控件
  • python列表的常用操作方法小结
  • Python中每次处理一个字符的5种方法
  • Python实现单词拼写检查

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python入门及进阶笔记 Python 内置函数小结
    • Python 专题一 函数的基础知识
    • Django集成百度富文本编辑器uEditor攻略
    • 用实例说明python的*args和**kwargs用法
    • TensorFlow——Checkpoint为模型添加检查点的实例
    • python原始套接字编程示例分享
    • Python中Django框架下的staticfiles使用简介
    • Python 出现错误TypeError: ‘NoneType’ object is not iterable解决办法
    • Python中MYSQLdb出现乱码的解决方法
    • python 算法 排序实现快速排序

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有