python使用BeautifulSoup分析网页信息的方法
本文实例讲述了python使用BeautifulSoup分析网页信息的方法。分享给大家供大家参考。具体如下:
这段python代码查找网页上的所有链接,分析所有的span标签,并查找class包含titletext的span的内容
#import the library used to query a website
import urllib2
#specify the url you want to query
url = "http://www.python.org"
#Query the website and return the html to the variable 'page'
page = urllib2.urlopen(url)
#import the Beautiful soup functions to parse the data returned from the website
from BeautifulSoup import BeautifulSoup
#Parse the html in the 'page' variable, and store it in Beautiful Soup format
soup = BeautifulSoup(page)
#to print the soup.head is the head tag and soup.head.title is the title tag
print soup.head
print soup.head.title
#to print the length of the page, use the len function
print len(page)
#create a new variable to store the data you want to find.
tags = soup.findAll('a')
#to print all the links
print tags
#to get all titles and print the contents of each title
titles = soup.findAll('span', attrs = { 'class' : 'titletext' })
for title in allTitles:
print title.contents
希望本文所述对大家的Python程序设计有所帮助。
相关推荐
-
python使用BeautifulSoup分页网页中超链接的方法
本文实例讲述了python使用BeautifulSoup分页网页中超链接的方法.分享给大家供大家参考.具体如下: python通过BeautifulSoup分页网页中的超级链接,这段python代码输出www.jb51.net主页上所有包含了jb51的url链接 from BeautifulSoup import BeautifulSoup import urllib2 import re url = urllib2.urlopen("http://www.jb51.net") con
-
python网络编程学习笔记(七):HTML和XHTML解析(HTMLParser、BeautifulSoup)
一.利用HTMLParser进行网页解析 具体HTMLParser官方文档可参考http://docs.python.org/library/htmlparser.html#HTMLParser.HTMLParser 1.从一个简单的解析例子开始 例1: test1.html文件内容如下: 复制代码 代码如下: <html> <head> <title> XHTML 与 HTML 4.01 标准没有太多的不同</title> </head> &l
-
Python使用BeautifulSoup库解析HTML基本使用教程
BeautifulSoup是Python的一个第三方库,可用于帮助解析html/XML等内容,以抓取特定的网页信息.目前最新的是v4版本,这里主要总结一下我使用的v3版本解析html的一些常用方法. 准备 1.Beautiful Soup安装 为了能够对页面中的内容进行解析,本文使用Beautiful Soup.当然,本文的例子需求较简单,完全可以使用分析字符串的方式. 执行 sudo easy_install beautifulsoup4 即可安装. 2.requests模块的安装 reque
-
python基于BeautifulSoup实现抓取网页指定内容的方法
本文实例讲述了python基于BeautifulSoup实现抓取网页指定内容的方法.分享给大家供大家参考.具体实现方法如下: # _*_ coding:utf-8 _*_ #xiaohei.python.seo.call.me:) #win+python2.7.x import urllib2 from bs4 import BeautifulSoup def jd(url): page = urllib2.urlopen(url) html_doc = page.read() soup = B
-
python BeautifulSoup设置页面编码的方法
在用BeautifulSoup进行抓取页面的时候,会各种各样的编码错误. 可以通过在beautifulsoup中指定字符编码,解决问题. 复制代码 代码如下: import urllib2 from BeautifulSoup import BeautifulSoup page = urllib2.urlopen('http://www.163.com'); soup = BeautifulSoup(page,from_encoding="gb2312") print
-
Python BeautifulSoup中文乱码问题的2种解决方法
解决方法一: 使用python的BeautifulSoup来抓取网页然后输出网页标题,但是输出的总是乱码,找了好久找到解决办法,下面分享给大家首先是代码 复制代码 代码如下: from bs4 import BeautifulSoupimport urllib2 url = 'http://www.jb51.net/'page = urllib2.urlopen(url) soup = BeautifulSoup(page,from_encoding="utf8")print soup
-
python利用beautifulSoup实现爬虫
以前讲过利用phantomjs做爬虫抓网页 http://www.jb51.net/article/55789.htm 是配合选择器做的 利用 beautifulSoup(文档 :http://www.crummy.com/software/BeautifulSoup/bs4/doc/)这个python模块,可以很轻松的抓取网页内容 # coding=utf-8 import urllib from bs4 import BeautifulSoup url ='http://www.baidu.
-
python 解析html之BeautifulSoup
复制代码 代码如下: # coding=utf-8 from BeautifulSoup import BeautifulSoup, Tag, NavigableString from SentenceSpliter import SentenceSpliter from os.path import basename,dirname,isdir,isfile from os import makedirs from shutil import copyfile import io import
-
Python网页解析利器BeautifulSoup安装使用介绍
python解析网页,无出BeautifulSoup左右,此是序言 安装 BeautifulSoup4以后的安装需要用eazy_install,如果不需要最新的功能,安装版本3就够了,千万别以为老版本就怎么怎么不好,想当初也是千万人在用的啊.安装很简单 复制代码 代码如下: $ wget "http://www.crummy.com/software/BeautifulSoup/download/3.x/BeautifulSoup-3.2.1.tar.gz" $ tar zxvf B
-
Windows8下安装Python的BeautifulSoup
运行环境:Windows 8.1 Python:2.7.6 在安装的时候,我使用的pip来进行安装,命令如下: 复制代码 代码如下: pip install beautifulsoup4 运行的时候,报错如下: 复制代码 代码如下: Exception: Traceback (most recent call last): File "J:\Program Files (x86)\Python\Python27\lib\site-packages\pip\basecomm .py"
-
python使用beautifulsoup从爱奇艺网抓取视频播放
复制代码 代码如下: import sysimport urllibfrom urllib import requestimport osfrom bs4 import BeautifulSoup class DramaItem: def __init__(self, num, title, url): self.num = num self.title = title self.url = url def __str__(self):
-
使用python BeautifulSoup库抓取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/
-
python BeautifulSoup使用方法详解
直接看例子: 复制代码 代码如下: #!/usr/bin/python# -*- coding: utf-8 -*-from bs4 import BeautifulSouphtml_doc = """<html><head><title>The Dormouse's story</title></head><body><p class="title"><b>T
随机推荐
- 详解Servlet之过滤器(Filter)
- Jquery遍历Json数据的方法
- 详解Docker创建Mysql容器并通过命令行连接到容器
- 一个JavaScript用逗号分割字符串实例
- 使用Python编写爬虫的基本模块及框架使用指南
- 35句细节 35句句句都点出了隐藏的信息
- jquery判断至少有一个checkbox被选中的方法
- PHP命名空间(Namespace)的使用详解
- Linux基础知识99问(五)
- C#十六进制字符串转十进制int的方法
- mingw编译的windows命令行贪吃蛇示例
- Android 代码JIT友好度检测工具
- 详解JavaScript的BUG和错误
- Luvit像Node.js一样写Lua应用
- python使用pipeline批量读写redis的方法
- MySql删除表中一行的实操方法
- Python2.7实现多进程下开发多线程示例
- Node.js安装详细步骤教程(Windows版)详解
- 微信小程序授权登陆及每次检查是否授权实例代码
- 如何在Windows中监视IO性能
