参考:http://www.freebuf.com/news/special/96763.html

相关资料:http://www.jb51.net/article/65287.htm

1、Python3 win7安装BeautifulSoup

BeautifulSoup中文文档:http://www.crummy.com/software/BeautifulSoup/bs3/documentation.zh.html

BeautifulSoup下载:http://www.crummy.com/software/BeautifulSoup/

解压,运行cmd执行:python setup.py install即可

2、导入beatifulsoup库 :from bs4 import BeautifulSoup

传入数据,建立对象: soup = BeautifulSoup(data),

操作soup,完成需求解析。

3、示例代码:

 from bs4 import BeautifulSoup
from urllib import request
import re web = request.urlopen('http://www.freebuf.com')
# 没有特别指明解析器,bs4使用了它认为最好的解析器,但是在不同的环境下运行,可能解析器是不一样的。
# 如果没有'html.parser',会有warning提示,表明了bs4的自动选择解析器来解析的特性。
soup = BeautifulSoup(web.read(),'html.parser')
tags_a = soup.find_all(name='a', attrs={'href': re.compile('^https?://')}) for tag_a in tags_a:
print(tag_a['href'])

4、利用BeautifulSoup获取网站的sitemap:

 # coding:utf-8
# 获取整个网站的sitemap import urllib.request
import urllib.error
from urllib.parse import urlparse
from bs4 import BeautifulSoup
import time
import datetime url = input('请输入扫描的url:')
domain = input('请输入包含的域名:')
sites = set() # 获取一个页面的所有url
def get_local_pages(url, domain):
pages = set()
global sites
repeat_time = 0 # 解析传入的url为后面相对路径拼接用
parse_url = urlparse(url) # 防止url读取卡住:自动重读5次
while True:
try:
print('Ready to Open the web!')
time.sleep(1)
print('Opening the web : %s' % url)
web = urllib.request.urlopen(url=url, timeout=20)
print('Success to Open the web!')
break
except urllib.error.URLError as e:
print('Open Url Error:',e)
print('Open url Failed!!!Repeat!')
time.sleep(1)
repeat_time += 1
if repeat_time == 5:
return soup = BeautifulSoup(web.read())
tags = soup.find_all(name='a') for tag in tags:
# 避免参数传递异常
try:
ret = tag['href']
except:
print('Maybe not the attr : href')
continue parse_page = urlparse(ret) # 1 url不为空(协议,域名,路径)
if parse_page[0] is '' and parse_page[1] is '' and parse_page[2] is '':
print('Bad Page(协议\域名\路径均为空):%s' % ret)
continue # 2 协议不为空,判断合法性
if parse_page[0] is not '' and 'http' not in parse_page[0]:
print('Bad Page(协议不合法,非http):%s' % ret)
continue # 3 域名不为空,domain要包含在域名中
if parse_page[1] is not '' and domain not in parse_page[1]:
print('Bad Page(域名不合法,非%s):%s' % (domain, ret))
continue # 4 协议为空,域名不为空(拼接ret),例如://caipiao.taobao.com
if parse_page[0] is '' and parse_page[1] is not '':
print('Fix page(仅域名存在): %s' % ret)
newpage = parse_url[0] + ':' + ret
if newpage not in sites:
print('Add Fix Page(拼接域名):%s' % newpage)
pages.add(newpage)
continue # 5 协议域名为空,路径不为空(拼接ret)
if parse_page[0] is '' and parse_page[1] is '':
print('Fix page(仅路径存在): %s' % ret)
temp_page = parse_url[0] + '://' + parse_url[1] + '/' + ret
# 保持URL的干净
newpage = temp_page[:8] + temp_page[8:].replace('//', '/')
if newpage not in sites:
print('Add Fix Page(拼接路径):%s' % newpage)
pages.add(newpage)
continue # 整理输出
newpage = ret
if newpage not in sites:
print('Add New Page:%s' % newpage)
pages.add(newpage) return pages # dfs 算法遍历全站(目前中小型网站可用,待完善)
def dfs(pages, domain):
global sites
if pages in sites:
return 'Success!' # visited = set()
# sites = set.union(sites,pages)
for page in pages:
if page not in sites:
sites.add(page)
get_pages = get_local_pages(page, domain)
dfs(get_pages, domain)
return t1 = datetime.datetime.now()
pages = get_local_pages(url, domain)
dfs(pages,domain)
text_name = domain + '全站扫描.txt'
with open(text_name, 'a') as f:
f.write('\n' + str(datetime.datetime.now()) + '\n')
for i in sites:
with open(text_name, 'a') as f:
f.write(i + '\n') with open(text_name, 'a') as f:
f.write('\n用时:' + str(datetime.datetime.now() - t1) + '\n') sitemap

sitemap

5、基本知识点

Bs4的基本api的使用,关于beautifulSoup的基本使用方法,我这里需要介绍在下面的脚本中我使用到的方法:

Soup = BeautifulSoup(data) #构建一个解析器

Tags = Soup.findAll(name,attr)

我们重点要讲findAll方法的两个参数:name和attr

Name:  指的是标签名,传入一个标签名的名称就可以返回所有固定名称的标签名

Attr:     是一个字典存储需要查找的标签参数,返回对应的标签

Tag.children      表示获取tag标签的所有子标签

Tag.string       表示获取tag标签内的所有字符串,不用一层一层索引下去寻找字符串

Tag.attrs[key]    表示获取tag标签内参数的键值对键为key的值

Tag.img         表示获取tag标签的标签名为img的自标签(一个)

6、利用BeautifulSoup获取58页面的指定信息(python2.7)

 #!/usr/bin/env python
# -*- coding: utf-8 -*- import urllib
import urllib2
from bs4 import BeautifulSoup url = 'http://ny.58.com/zufang/24584108096437x.shtml?qq-pf-to=pcqq.c2c' # rq = urllib2.Request(url)
# print rq
rp = urllib.urlopen(url)
html = rp.read()
soup = BeautifulSoup(html) # 获取标题
title = soup.find_all(name='h1', attrs={'class': 'main-title font-heiti'})
for data in title:
data_title = data.get_text()
print data_title # 获取租金
primary = soup.find_all(name='em', attrs={'class': 'house-price'})
for data in primary:
data_primary = data.get_text()
print data_primary # 获取房屋
house_type = soup.find_all(name='div', attrs={'class': 'fl house-type c70'})
for data in house_type:
temp_type = data.get_text().replace('-', ' ')
temp_type = ' '.join(temp_type.split())
print temp_type
# data_type_list = []
# for d in temp_type:
# data_type_list.append(d)
# print data_type_list # 获取小区
xiaoqu = soup.find_all(name='div', attrs={'class': 'fl xiaoqu c70'})
for data in xiaoqu:
data_xiaoqu = data.get_text().strip()
print data_xiaoqu # 获取配置
config = soup.find_all(name='li', attrs={'class': 'house-primary-content-li clearfix person-config'})
for data in config:
data_config = data.div.get_text().replace('-',' ')
data_config = ' '.join(data_config.split())
print data_config # 获取联系人
contact = soup.find_all(name='li', attrs={'class': 'house-primary-content-li clearfix person-contact'})
for data in contact:
data_contact = data.div.span.get_text()
print data_contact # 写入文件
# with open('58_test1.txt','w') as f:
# f.write('标题:'+data_title.decode('gbk'))
# f.write('租金:' + data_primary)

BeautifulSoup的更多相关文章

  1. Python爬虫小白入门(三)BeautifulSoup库

    # 一.前言 *** 上一篇演示了如何使用requests模块向网站发送http请求,获取到网页的HTML数据.这篇来演示如何使用BeautifulSoup模块来从HTML文本中提取我们想要的数据. ...

  2. 使用beautifulsoup与requests爬取数据

    1.安装需要的库 bs4 beautifulSoup  requests lxml如果使用mongodb存取数据,安装一下pymongo插件 2.常见问题 1> lxml安装问题 如果遇到lxm ...

  3. BeautifulSoup :功能使用

    # -*- coding: utf-8 -*- ''' # Author : Solomon Xie # Usage : 测试BeautifulSoup一些用法及容易出bug的地方 # Envirom ...

  4. BeautifulSoup研究一

    BeautifulSoup的文档见 https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/ 其中.contents 会将换行也记录为一个子节 ...

  5. BeautifulSoup Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER.

    BeautifulSoup很赞的东西 最近出现一个问题:Python 3.3 soup=BeautifulSoup(urllib.request.urlopen(url_path),"htm ...

  6. beautifulSoup(1)

    import re from bs4 import BeautifulSoupdoc = ['<html><head><title>Page title</t ...

  7. python BeautifulSoup模块的简要介绍

    常用介绍: pip install beautifulsoup4 # 安装模块 from bs4 import BeautifulSoup # 导入模块 soup = BeautifulSoup(ht ...

  8. BeautifulSoup 的用法

    转自:http://cuiqingcai.com/1319.html Beautiful Soup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,如果我们不安装它,则 Python ...

  9. BeautifulSoup的选择器

    用BeautifulSoup查找指定标签(元素)的时候,有几种方法: soup=BeautifulSoup(html) 1.soup.find_all(tagName),返回一个指定Tag元素的列表 ...

随机推荐

  1. 【ASP.NET】复制单个文件同时到多个目录

    有时候,当我们更新了一个dll文件后,需要将该dll文件复制到到不同的文件夹中,手动操作会很麻烦,因此可以考虑利用程序实现. 利用powershell批量复制 示例代码如下: $source=&quo ...

  2. html JS 打开本地程序及文件

    在网页打开本地应用程序示例: 一.在本地注册表自定义协议:以自定义调用Viso为例 1.在HKEY_CLASSES_ROOT下添加项ZVISIO. 2.修改ZVISIO项下的"(默认)&qu ...

  3. Tomcat7.0+的JNDI问题

    上次搭建spring+springmvc+mybatis框架时用的第三方连接池jar包,但是部署到tomcat中后访问没有问题,但是启动时报了个JNDI的错,我没用JNDI你给我报什么,fuck!把错 ...

  4. Python的方法解析顺序(MRO)

    mro即method resolution order,主要用于在多继承时判断调的属性的路径(来自于哪个类). http://blog.csdn.net/imzoer/article/details/ ...

  5. 纯css实现二级导航菜单效果,通过简单的鼠标事件操作页面元素样式变换实现二级导航菜单的功能,非常简单实用,

    HTML代码如下 <!-- 头部导航栏开始--><div id="nav"><dl class="sy"><dt> ...

  6. How to use *args and **kwargs in Python

    Or, How to use variable length argument lists in Python. The special syntax, *args and **kwargs in f ...

  7. TDD学习笔记【一】----序言

    提到TDD大多数程序员的疑问: 为什么我要写两份程序? 为什么我要写程序来验证我已经知道的结果? 我又不是SA,可能也不懂domain,怎么产生一开始的test case? 最后的感想就变成是: 1. ...

  8. 前端试题本(HTML+CSS篇)

    CS1. 下面关于IE.FF下面CSS的解释区别描述正确的有?(不定项)CS2请选出结构正确的选项CS3.下面哪些是HTML5 新增的表单元素?CS4在使用table表现数据时,有时候表现出来的会比自 ...

  9. PL/SQL客户端中执行insert语句,插入中文乱码

    问题描述:在PL/SQL客户端中执行insert语句,插入中文乱码 解决方案: 1.执行脚本 select userenv('language') from dual;    结果为AMERICAN_ ...

  10. Java集合框架练习-计算表达式的值

    最近在看<算法>这本书,正好看到一个计算表达式的问题,于是就打算写一下,也正好熟悉一下Java集合框架的使用,大致测试了一下,没啥问题. import java.util.*; /* * ...