Python BeautifulSoup的使用
2016-12-27 00:00
423 查看
摘要: 回顾python Beautiful Soup的使用,主要是对一些方法和参数进行介绍
BeautifulSoup提供一些简单的、python式的函数用来处理导航、搜索、修改分析树等功能。它是一个工具箱,通过解析文档为用户提供需要抓取的数据,因为简单,所以不需要多少代码就可以写出一个完整的应用程序;
BeautifulSoup自动将输入文档转换为Unicode编码,输出文档转换为utf-8编码。你不需要考虑编码方式,除非文档没有指定一个编码方式,这时,BeautifulSoup就不能自动识别编码方式了。然后,你仅仅需要说明一下原始编码方式就可以了;
BeautifulSoup已成为和lxml、html6lib一样出色的python解释器,为用户灵活地提供不同的解析策略或强劲的速度。
下载完成后解压缩到,python的安装目录;
运行cmd,进入bs4文件夹;
执行 setup.py build;
执行 setup.py install 即可以完成安装;
安装完成后要检查是否安转成功。其实没必要这么复杂,如果有pip 我们直接pip安装即可。
Tag:Tag通俗点讲就是HTML中的一个个标签。Beautiful Soup是如何方便的获取Tags?首先我们需要创建一个suop对象如:soup = BeautifulSoup(html文件,"html.parser")
BeautifulSoup:BeautifulSoup 对象表示的是一个文档的全部内容.大部分时候,可以把它当作 Tag 对象,是一个特殊的 Tag,我们可以分别获取它的类型,名称。
NavigableString:我们已经获得标签的内容,怎么样获取标签内部的文字呢?用.string。它的类型是一个 NavigableString,翻译过来叫可以遍历的字符串。
Comment:Comment 对象是一个特殊类型的 NavigableString 对象,其实输出的内容仍然不包括注释符号,但是如果不好好处理它,可能会对我们的文本处理造成意想不到的麻烦.
** 遍历所有子孙节点**。要点:
遍历节点内容。如果tag只有一个 NavigableString 类型子节点,那么这个tag可以
使用 .string 得到子节点。如果一个tag仅有一个子节点,那么这个tag也可以使用 .string 方法,输出结果与当前唯一子节点的 .string 结果相同。通俗点说就是:如果一个标签里面没有标签了,那么 .string 就会返回标签里面的内容。如果标签里面只有唯一的一个标签了,那么 .string 也会返回最里面的内容。如果tag包含了多个子节点,tag就无法确定,string 方法应该调用哪个子节点的内容, .string 的输出结果是 None。
那么我们就需要利用
遍历文档树父节点。使用
遍历兄弟节点。兄弟节点可以理解为和本节点处在统一级的节点,
.previous_siblings
比如 head 节点为:<head><title>The Dormouse's story</title></head>
那么它的下一个节点为:print(soup.head.next_element)
输出:<title>The Dormouse‘s story</title> 并没有层次关系
print(soup.find_all("b"))
输出:[<b>The Dormouse's story</b>]
import re
for tag in soup.find_all(re.compile("^a")):
print(tag.name)
输出:
a
a
a
for tag in (soup.find_all(["p","b"])):
print(tag.name)
输出:
p
b
p
p
for tag in (soup.find_all(True)):
print(tag.name)
输出:
html
head
title
body
p
b
p
def has_tag_but_no_id(tag):
return tag.has_attr("class") and not tag.has_attr("id")
for tagName in (soup.find_all(has_tag_but_no_id)):
print(tagName.name)
data_soup = BeautifulSoup("<div data-foo = 'value'>foo!</div>","html.parser")
print(data_soup.find_all("data-foo"))
输出:[]
print(data_soup.find_all(attrs = {"data-foo":"value"}))
输出:
[<div data-foo="value">foo!</div>]
print(soup.find_all(text="Lacie"))
输出:['Lacie']
print(soup.find_all(text=["Lacie","Tillie"]))
输出:['Lacie', 'Tillie']
print(soup.select("title"))
输出:[<title>The Dormouse‘s story</title>](返回列表形式)
print(soup.select(".sister"))
输出:[<a class="sister" href="http://example.com/elsie" id="link1"><!--Elsie--></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
print(soup.select("#link2"))
输出:[<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]
print(soup.select("p #link1"))
输出:[<a class="sister" href="http://example.com/elsie" id="link1"><!--Elsie--></a>]
print(soup.select("a[class='sister']")) //是不是很类似于jquery中的筛选
输出:
[<a class="sister" href="http://example.com/elsie" id="link1"><!--Elsie--></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
#url = http://wuhan.anjuke.com/sale/
from bs4 import BeautifulSoup
import requests
import time
import pymysql
import re
url_QX = []
def url_qx(url):
wb_data = requests.get(url).text
soup = BeautifulSoup(wb_data,"html.parser")
information = soup.select("div.div-border.items-list > div.items > span.elems-l > a")
url_qx("http://wuhan.anjuke.com/sale/")
url_qxTop = url_QX[0:15] #这是武昌,汉口等区,我们最后希望得到的是 武昌所有位置(比如说沙湖等位置)
#print(url_qxTop)
url_finally = [] #建立一个临时列表,用来进行地址的截取
def url_qx_finally(urlDict):
for i in range(0,15):
eachUrl = urlDict[i]["url"]
wb_data = requests.get(eachUrl).text
wb_soup = BeautifulSoup(wb_data,"html.parser")
wb_information = wb_soup.select("div.div-border.items-list > div.items > span.elems-l > div.sub-items > a")
#以武昌区为例,我们找到了所有武昌区下面更详细的区
#<div class="items"><span class="item-title">区域:</span><span class="elems-l">
# <a href='http://wuhan.anjuke.com/sale/' class='' >全部</a><span class="selected-item">武昌</span><a href='http://wuhan.anjuke.com/sale/hongshana/' class='' >洪山</a><a href='http://wuhan.anjuke.com/sale/jiangan/' class='' >江岸</a><a href='http://wuhan.anjuke.com/sale/jianghana/' class='' >江汉</a><a href='http://wuhan.anjuke.com/sale/qiaokou/' class='' >硚口</a><a href='http://wuhan.anjuke.com/sale/hanyang/' class='' >汉阳</a><a href='http://wuhan.anjuke.com/sale/dongxihu/' class='' >东西湖</a><a href='http://wuhan.anjuke.com/sale/qingshan/' class='' >青山</a><a href='http://wuhan.anjuke.com/sale/jiangxiat/' class='' >江夏</a><a href='http://wuhan.anjuke.com/sale/zhuankoukaifaqu/' class='' >沌口开发区</a><a href='http://wuhan.anjuke.com/sale/huangpiz/' class='' >黄陂</a><a href='http://wuhan.anjuke.com/sale/caidianz/' class='' >蔡甸</a><a href='http://wuhan.anjuke.com/sale/hannanz/' class='' >汉南</a><a href='http://wuhan.anjuke.com/sale/xinzhouz/' class='' >新洲</a><a href='http://wuhan.anjuke.com/sale/qitao/' class='' >其他</a>
# <div class="sub-items"><span class="selected-item">全部</span>
# <span class="sub-letter-item" style="color: #f60;margin-right: 3px;">D</span>
# <a href='http://wuhan.anjuke.com/sale/donghudongting/' class=''>东湖东亭</a>
# <a href='http://wuhan.anjuke.com/sale/dongtingwuchanga/?from=shangquan' class=''>东亭</a><
# a href='http://wuhan.anjuke.com/sale/dingziqiao/?from=shangquan' class=''>丁字桥</a>
# <span class="sub-letter-item" style="color: #f60;margin-right: 3px;">F</span>
# <a href='http://wuhan.anjuke.com/sale/fujiapo/' class=''>傅家坡</a>
# <span class="sub-letter-item" style="color: #f60;margin-right: 3px;">J</span>
# <a href='http://wuhan.anjuke.com/sale/jiyuqiao/' class=''>积玉桥</a>
# <span class="sub-letter-item" style="color: #f60;margin-right: 3px;">L</span><a href='http://wuhan.anjuke.com/sale/liangdaojie/' class=''>粮道街</a><span class="sub-letter-item" style="color: #f60;margin-right: 3px;">N</span><a href='http://wuhan.anjuke.com/sale/nanhuhuayuan/' class=''>南湖花园</a><span class="sub-letter-item" style="color: #f60;margin-right: 3px;">S</span><a href='http://wuhan.anjuke.com/sale/shuiguohu/' class=''>水果湖</a><a href='http://wuhan.anjuke.com/sale/simenkou/' class=''>司门口</a><a href='http://wuhan.anjuke.com/sale/shouyilu/?from=shangquan' class=''>首义路</a><span class="sub-letter-item" style="color: #f60;margin-right: 3px;">T</span><a href='http://wuhan.anjuke.com/sale/tuanjiedadao/' class=''>团结大道</a><span class="sub-letter-item" style="color: #f60;margin-right: 3px;">W</span><a href='http://wuhan.anjuke.com/sale/wuchanghuochezhan/' class=''>武昌火车站</a><a href='http://wuhan.anjuke.com/sale/wuchangzhoubian/' class=''>武昌周边</a><a href='http://wuhan.anjuke.com/sale/wutaizhafenghuo/' class=''>武泰闸烽火</a><a href='http://wuhan.anjuke.com/sale/wutaizha/?from=shangquan' class=''>武泰闸</a><span class="sub-letter-item" style="color: #f60;margin-right: 3px;">X</span><a href='http://wuhan.anjuke.com/sale/xiaodongmen/' class=''>小东门</a><a href='http://wuhan.anjuke.com/sale/xudong/' class=''>徐东</a><a href='http://wuhan.anjuke.com/sale/xujiapeng/' class=''>徐家棚</a><span class="sub-letter-item" style="color: #f60;margin-right: 3px;">Y</span><a href='http://wuhan.anjuke.com/sale/yangyuand/' class=''>杨园</a><a href='http://wuhan.anjuke.com/sale/yuemachangshouyi/' class=''>阅马场首义</a><span class="sub-letter-item" style="color: #f60;margin-right: 3px;">Z</span><a href='http://wuhan.anjuke.com/sale/zhonghualud/' class=''>中华路</a><a href='http://wuhan.anjuke.com/sale/ziyanglu/' class=''>紫阳路</a><a href='http://wuhan.anjuke.com/sale/zhongbeilu/' class=''>中北路</a><a href='http://wuhan.anjuke.com/sale/zhongnandingziqiao/' class=''>中南丁字桥</a></div></span></div><!-- 区域 end-->
for wb_url in wb_information:
finallyData = {
"url":wb_url.get("href"),
"address":wb_url.get_text()
}
url_finally.append(finallyData)
url_qx_finally(url_qxTop) #这里我们就得到了所有的url
#print(url_finally)
#接下来我们连接mysql
#打开数据库连接
conn = pymysql.connect(
host = "localhost",
user = "用户名",
password = "密码",
port = 3306,
db = "mysql",
charset = "UTF8"
)
#使用cursor()方法获取操作游标
cursor = conn.cursor()
#cursor.execute("DROP TABLE if 安居客20160917 exs")
sql= """
create table 安居客20160917(
房价 CHAR (30),
地址 CHAR (200))
"""
cursor.execute(sql)
#cursor.close()
#cursor.execute("select * from 安居客20160917")
#results = cursor.fetchall()
#print(results)
for urlOrignal in url_finally:
for i in range(1):
url = (urlOrignal["url"] + "p{}/").format(i)
data = requests.get(url).text
soup = BeautifulSoup(data, "html.parser")
address = soup.select("div.house-details > div > span.comm-address")
#print(len(address))
prices = soup.select(("div.house-details > div > span:nth-of-type(3)"))
#print(len(prices))
#break
# print(price)
#if prices == None:
#continue
#else:
for price, address in zip(prices, address):
price = price.get_text()
#print(price)
address = address.get("title")
#print(address)
try:
cursor.execute("insert into 安居客20160917 (房价,地址) values (%s,%s)",(price,address))
except:
conn.rollback()
conn.commit()
#print(url)
url = urlOrignal
cursor.close()
conn.close()
1.BeautifulSoup的可以干什么?
BeautifulSoup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式;BeautifulSoup提供一些简单的、python式的函数用来处理导航、搜索、修改分析树等功能。它是一个工具箱,通过解析文档为用户提供需要抓取的数据,因为简单,所以不需要多少代码就可以写出一个完整的应用程序;
BeautifulSoup自动将输入文档转换为Unicode编码,输出文档转换为utf-8编码。你不需要考虑编码方式,除非文档没有指定一个编码方式,这时,BeautifulSoup就不能自动识别编码方式了。然后,你仅仅需要说明一下原始编码方式就可以了;
BeautifulSoup已成为和lxml、html6lib一样出色的python解释器,为用户灵活地提供不同的解析策略或强劲的速度。
2.bs4在windows环境下的安装
到官网上下载,我下的版本是4.5.1;下载完成后解压缩到,python的安装目录;
运行cmd,进入bs4文件夹;
执行 setup.py build;
执行 setup.py install 即可以完成安装;
安装完成后要检查是否安转成功。其实没必要这么复杂,如果有pip 我们直接pip安装即可。
3.四大对象种类
Beautiful Soup将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象,所有对象可以归纳为4种:Tag:Tag通俗点讲就是HTML中的一个个标签。Beautiful Soup是如何方便的获取Tags?首先我们需要创建一个suop对象如:soup = BeautifulSoup(html文件,"html.parser")
soup.title # <title>The Dormouse's story</title> soup.title.name # u'title' soup.title.string # u'The Dormouse's story' soup.title.parent.name # u'head' soup.p # <p class="title"><b>The Dormouse's story</b></p> soup.p['class'] # u'title' soup.a # <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a> soup.find_all('a') # [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, # <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, # <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>] soup.find(id="link3") # <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
BeautifulSoup:BeautifulSoup 对象表示的是一个文档的全部内容.大部分时候,可以把它当作 Tag 对象,是一个特殊的 Tag,我们可以分别获取它的类型,名称。
NavigableString:我们已经获得标签的内容,怎么样获取标签内部的文字呢?用.string。它的类型是一个 NavigableString,翻译过来叫可以遍历的字符串。
print(soup.p.string) print(type(soup.p.string))//结果会发现,打印出来的结果其实就是一个NavigableString类型
Comment:Comment 对象是一个特殊类型的 NavigableString 对象,其实输出的内容仍然不包括注释符号,但是如果不好好处理它,可能会对我们的文本处理造成意想不到的麻烦.
//我们找一个带有注释的标签: print(soup.a) //运行结果(一个带有注释的a标签) <a class="sister" href="http://example.com/elsie" id="link1"><!--Elsie--></a> print(soup.a.string) //注:我们在打印.string的时候,最好先判断(根据类型), //避免运行结果(输出了注释,但是没有注释符号) //Elsie注释与非注释混淆打印出来了 print(type(soup.a.string)) //运行结果(类型是Comment) <class 'bs4.element.Comment'>
4.遍历文档树
遍历直接子节点。这里包含两个属性:.contents、
.children属性,tag 的 .content 属性可以将tag的子节点以列表的方式输出,.children它返回的不是一个 list,不过我们可以通过遍历获取所有子节点。我们打印输出 .children 看一下,可以发现它是一个 list 生成器对象。
查看其类型:print(soup.children) 运行结果:<list_iterator object at 0x0000000000E24438>
** 遍历所有子孙节点**。要点:
.descendants属性。.contents 和 .children 属性仅包含tag的直接子点,.descendants 属性可以对所有tag的子孙节点进行递归循环,和 children类似,我们也需要遍历获取其中的内容。
遍历节点内容。如果tag只有一个 NavigableString 类型子节点,那么这个tag可以
使用 .string 得到子节点。如果一个tag仅有一个子节点,那么这个tag也可以使用 .string 方法,输出结果与当前唯一子节点的 .string 结果相同。通俗点说就是:如果一个标签里面没有标签了,那么 .string 就会返回标签里面的内容。如果标签里面只有唯一的一个标签了,那么 .string 也会返回最里面的内容。如果tag包含了多个子节点,tag就无法确定,string 方法应该调用哪个子节点的内容, .string 的输出结果是 None。
print(soup.head.string) //输出: The Dormouse's story print(soup.title.string) //输出:The Dormouse's story //如果tag包含太多子节点,例如: print(soup.html.string) //输出:None
那么我们就需要利用
.strings、
.stripped_strings属性遍历多个内容。.strings获取多个内容,不过需要遍历获取;.strings 输出的字符串中可能包含了很多空格或空行,用 .stripped_strings 可以去除多余空白内容。
遍历文档树父节点。使用
.parent属性可以获得节点的父节点;通过元素的
.parents属性可以递归得到元素的所有父辈节点。
content = soup.head.title.string for parent in content.parents: print(parent.name) 输出: title head html [document]
遍历兄弟节点。兄弟节点可以理解为和本节点处在统一级的节点,
.next_sibling属性获取了该节点的下一个兄弟节点,
.previous_sibling则与之相反,如果节点不存在,则返回 None。**注意:**实际文档中的tag的
.next_sibling和
.previous_sibling属性通常是字符串或空白,因为空白或者换行也可以被视作一个节点,所以得到的结果可能是空白或者换行。通过
.next_siblings和```
.previous_siblings
1. **遍历文档树的前后节点**。```.next_element ```,```.previous_element ```属性与``` .next_sibling```,```.previous_sibling``` 不同,它并不是针对于兄弟节点,而是在所有节点,不分层次。
比如 head 节点为:<head><title>The Dormouse's story</title></head>
那么它的下一个节点为:print(soup.head.next_element)
输出:<title>The Dormouse‘s story</title> 并没有层次关系
### 5.搜索文档树。 主要是```find_all()```方法搜索当前tag的所有tag子节点,并判断是否符合过滤器的条件。```find_all( name , attrs , recursive , text , **kwargs )``` 1. name参数:name 参数可以查找所有名字为 name 的tag,字符串对象会被自动忽略掉。 - A.传字符串:最简单的过滤器是字符串。在搜索方法中传入一个字符串参数,Beautiful Soup会查找与字符串完整匹配的内容
print(soup.find_all("b"))
输出:[<b>The Dormouse's story</b>]
- B.传正则表达式:如果传入正则表达式作为参数,BeautifulSoup会通过正则表达式的 match() 来匹配内容.下面例子中找出所有以b开头的标签,这表示<body>和<b>标签都应该被找到。
import re
for tag in soup.find_all(re.compile("^a")):
print(tag.name)
输出:
a
a
a
- C.传列表:如果传入列表参数,Beautiful Soup会将与列表中任一元素匹配的内容返回.下面代码找到文档中所有<a>标签和<b>标签。
for tag in (soup.find_all(["p","b"])):
print(tag.name)
输出:
p
b
p
p
- 传 True:True 可以匹配任何值,下面代码查找到所有的tag,但是不会返回字符串节点。
for tag in (soup.find_all(True)):
print(tag.name)
输出:
html
head
title
body
p
b
p
- 传方法:如果没有合适过滤器,那么还可以定义一个方法,方法只接受一个元素参数,如果这个方法返回 True 表示当前元素匹配并且被找到,如果不是则反回 False。下面方法校验了当前元素,如果包含 class 属性却不包含 id 属性,那么将返回 True。
def has_tag_but_no_id(tag):
return tag.has_attr("class") and not tag.has_attr("id")
for tagName in (soup.find_all(has_tag_but_no_id)):
print(tagName.name)
1. keyword参数 - 有些tag属性在搜索不能使用,比如HTML5中的 data-* 属性
data_soup = BeautifulSoup("<div data-foo = 'value'>foo!</div>","html.parser")
print(data_soup.find_all("data-foo"))
输出:[]
- 但是可以通过 find_all() 方法的 attrs 参数定义一个字典参数来搜索包含特殊属性的tag。
print(data_soup.find_all(attrs = {"data-foo":"value"}))
输出:
[<div data-foo="value">foo!</div>]
1. text参数:通过 text 参数可以搜索文档中的字符串内容。与 name 参数的可选值一样, text 参数接受 字符串 , 正则表达式 , 列表, True。
print(soup.find_all(text="Lacie"))
输出:['Lacie']
print(soup.find_all(text=["Lacie","Tillie"]))
输出:['Lacie', 'Tillie']
1. recursive 参数:调用tag的find_all() 方法时,BeautifulSoup会检索当前tag的所有子孙节点,如果只想搜索tag的直接子节点,可以使用参数```recursive=False```。 1. **类似于find_all()的一些其他方法** - find_previous_siblings(),find_previous_sibling():这2个方法通过 .previous_siblings 属性对当前 tag 的前面解析的兄弟 tag 节点进行迭代, find_previous_siblings() 方法返回所有符合条件的前面的兄弟节点,find_previous_sibling() 方法返回第一个符合条件的前面的兄弟节点; - find_all_next() find_next():这2个方法通过 .next_elements 属性对当前 tag 的之后的 tag 和字符串进行迭代, find_all_next() 方法返回所有符合条件的节点, find_next() 方法返回第一个符合条件的节点; - find_all_previous() 和 find_previous():这2个方法通过 .previous_elements 属性对当前节点前面的 tag 和字符串进行迭代, find_all_previous() 方法返回所有符合条件的节点, find_previous()方法返回第一个符合条件的节点。 ### 6.css选择器 1. 我们在写 CSS 时,标签名不加任何修饰,类名前加点,id名前加 #,在这里我们也可以利用类似的方法来筛选元素,用到的方法是 soup.select(),返回类型是 list。
print(soup.select("title"))
输出:[<title>The Dormouse‘s story</title>](返回列表形式)
1. 通过类名查找
print(soup.select(".sister"))
输出:[<a class="sister" href="http://example.com/elsie" id="link1"><!--Elsie--></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
1. id和组合查找 - 通过id查找
print(soup.select("#link2"))
输出:[<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]
- 组合查找(组合查找即和写 class 文件时,标签名与类名、id名进行的组合原理是一样的,例如查找 p 标签中,id 等于 link1的内容,二者需要用空格分开)。
print(soup.select("p #link1"))
输出:[<a class="sister" href="http://example.com/elsie" id="link1"><!--Elsie--></a>]
1. 属性查找:查找时还可以加入属性元素,属性需要用中括号括起来,注意属性和标签属于同一节点,所以中间不能加空格,否则会无法匹配到。
print(soup.select("a[class='sister']")) //是不是很类似于jquery中的筛选
输出:
[<a class="sister" href="http://example.com/elsie" id="link1"><!--Elsie--></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
### 7.分享一下用BeautifulSoup爬虫的小例子 - 部分源码如下:
#url = http://wuhan.anjuke.com/sale/
from bs4 import BeautifulSoup
import requests
import time
import pymysql
import re
url_QX = []
def url_qx(url):
wb_data = requests.get(url).text
soup = BeautifulSoup(wb_data,"html.parser")
information = soup.select("div.div-border.items-list > div.items > span.elems-l > a")
# 我们要找的就是下面的这些url,实际上语句执行的 查找出来的结果有多的,没关系,我们只取我们要的 #<div class="items"><span class="item-title">区域:</span><span class="elems-l"> # <span class="selected-item">全部</span> # <a href='http://wuhan.anjuke.com/sale/wuchanga/' class='' >武昌</a> # <a href='http://wuhan.anjuke.com/sale/hongshana/' class='' >洪山</a>< # a href='http://wuhan.anjuke.com/sale/jiangan/' class='' >江岸</a> # <a href='http://wuhan.anjuke.com/sale/jianghana/' class='' >江汉</a> # <a href='http://wuhan.anjuke.com/sale/qiaokou/' class='' >硚口</a> # <a href='http://wuhan.anjuke.com/sale/hanyang/' class='' >汉阳</a> # <a href='http://wuhan.anjuke.com/sale/dongxihu/' class='' >东西湖</a> # <a href='http://wuhan.anjuke.com/sale/qingshan/' class='' >青山</a> # <a href='http://wuhan.anjuke.com/sale/jiangxiat/' class='' >江夏</a> # <a href='http://wuhan.anjuke.com/sale/zhuankoukaifaqu/' class='' >沌口开发区</a> # <a href='http://wuhan.anjuke.com/sale/huangpiz/' class='' >黄陂</a> # <a href='http://wuhan.anjuke.com/sale/caidianz/' class='' >蔡甸</a> # <a href='http://wuhan.anjuke.com/sale/hannanz/' class='' >汉南</a> # <a href='http://wuhan.anjuke.com/sale/xinzhouz/' class='' >新洲</a> # <a href='http://wuhan.anjuke.com/sale/qitao/' class='' >其他</a></span></div><!-- 区域 end--> for url in information: data = { "url":url.get("href"), "address:":url.get_text() } #print(data) url_QX.append(data)
url_qx("http://wuhan.anjuke.com/sale/")
url_qxTop = url_QX[0:15] #这是武昌,汉口等区,我们最后希望得到的是 武昌所有位置(比如说沙湖等位置)
#print(url_qxTop)
url_finally = [] #建立一个临时列表,用来进行地址的截取
def url_qx_finally(urlDict):
for i in range(0,15):
eachUrl = urlDict[i]["url"]
wb_data = requests.get(eachUrl).text
wb_soup = BeautifulSoup(wb_data,"html.parser")
wb_information = wb_soup.select("div.div-border.items-list > div.items > span.elems-l > div.sub-items > a")
#以武昌区为例,我们找到了所有武昌区下面更详细的区
#<div class="items"><span class="item-title">区域:</span><span class="elems-l">
# <a href='http://wuhan.anjuke.com/sale/' class='' >全部</a><span class="selected-item">武昌</span><a href='http://wuhan.anjuke.com/sale/hongshana/' class='' >洪山</a><a href='http://wuhan.anjuke.com/sale/jiangan/' class='' >江岸</a><a href='http://wuhan.anjuke.com/sale/jianghana/' class='' >江汉</a><a href='http://wuhan.anjuke.com/sale/qiaokou/' class='' >硚口</a><a href='http://wuhan.anjuke.com/sale/hanyang/' class='' >汉阳</a><a href='http://wuhan.anjuke.com/sale/dongxihu/' class='' >东西湖</a><a href='http://wuhan.anjuke.com/sale/qingshan/' class='' >青山</a><a href='http://wuhan.anjuke.com/sale/jiangxiat/' class='' >江夏</a><a href='http://wuhan.anjuke.com/sale/zhuankoukaifaqu/' class='' >沌口开发区</a><a href='http://wuhan.anjuke.com/sale/huangpiz/' class='' >黄陂</a><a href='http://wuhan.anjuke.com/sale/caidianz/' class='' >蔡甸</a><a href='http://wuhan.anjuke.com/sale/hannanz/' class='' >汉南</a><a href='http://wuhan.anjuke.com/sale/xinzhouz/' class='' >新洲</a><a href='http://wuhan.anjuke.com/sale/qitao/' class='' >其他</a>
# <div class="sub-items"><span class="selected-item">全部</span>
# <span class="sub-letter-item" style="color: #f60;margin-right: 3px;">D</span>
# <a href='http://wuhan.anjuke.com/sale/donghudongting/' class=''>东湖东亭</a>
# <a href='http://wuhan.anjuke.com/sale/dongtingwuchanga/?from=shangquan' class=''>东亭</a><
# a href='http://wuhan.anjuke.com/sale/dingziqiao/?from=shangquan' class=''>丁字桥</a>
# <span class="sub-letter-item" style="color: #f60;margin-right: 3px;">F</span>
# <a href='http://wuhan.anjuke.com/sale/fujiapo/' class=''>傅家坡</a>
# <span class="sub-letter-item" style="color: #f60;margin-right: 3px;">J</span>
# <a href='http://wuhan.anjuke.com/sale/jiyuqiao/' class=''>积玉桥</a>
# <span class="sub-letter-item" style="color: #f60;margin-right: 3px;">L</span><a href='http://wuhan.anjuke.com/sale/liangdaojie/' class=''>粮道街</a><span class="sub-letter-item" style="color: #f60;margin-right: 3px;">N</span><a href='http://wuhan.anjuke.com/sale/nanhuhuayuan/' class=''>南湖花园</a><span class="sub-letter-item" style="color: #f60;margin-right: 3px;">S</span><a href='http://wuhan.anjuke.com/sale/shuiguohu/' class=''>水果湖</a><a href='http://wuhan.anjuke.com/sale/simenkou/' class=''>司门口</a><a href='http://wuhan.anjuke.com/sale/shouyilu/?from=shangquan' class=''>首义路</a><span class="sub-letter-item" style="color: #f60;margin-right: 3px;">T</span><a href='http://wuhan.anjuke.com/sale/tuanjiedadao/' class=''>团结大道</a><span class="sub-letter-item" style="color: #f60;margin-right: 3px;">W</span><a href='http://wuhan.anjuke.com/sale/wuchanghuochezhan/' class=''>武昌火车站</a><a href='http://wuhan.anjuke.com/sale/wuchangzhoubian/' class=''>武昌周边</a><a href='http://wuhan.anjuke.com/sale/wutaizhafenghuo/' class=''>武泰闸烽火</a><a href='http://wuhan.anjuke.com/sale/wutaizha/?from=shangquan' class=''>武泰闸</a><span class="sub-letter-item" style="color: #f60;margin-right: 3px;">X</span><a href='http://wuhan.anjuke.com/sale/xiaodongmen/' class=''>小东门</a><a href='http://wuhan.anjuke.com/sale/xudong/' class=''>徐东</a><a href='http://wuhan.anjuke.com/sale/xujiapeng/' class=''>徐家棚</a><span class="sub-letter-item" style="color: #f60;margin-right: 3px;">Y</span><a href='http://wuhan.anjuke.com/sale/yangyuand/' class=''>杨园</a><a href='http://wuhan.anjuke.com/sale/yuemachangshouyi/' class=''>阅马场首义</a><span class="sub-letter-item" style="color: #f60;margin-right: 3px;">Z</span><a href='http://wuhan.anjuke.com/sale/zhonghualud/' class=''>中华路</a><a href='http://wuhan.anjuke.com/sale/ziyanglu/' class=''>紫阳路</a><a href='http://wuhan.anjuke.com/sale/zhongbeilu/' class=''>中北路</a><a href='http://wuhan.anjuke.com/sale/zhongnandingziqiao/' class=''>中南丁字桥</a></div></span></div><!-- 区域 end-->
for wb_url in wb_information:
finallyData = {
"url":wb_url.get("href"),
"address":wb_url.get_text()
}
url_finally.append(finallyData)
url_qx_finally(url_qxTop) #这里我们就得到了所有的url
#print(url_finally)
#接下来我们连接mysql
#打开数据库连接
conn = pymysql.connect(
host = "localhost",
user = "用户名",
password = "密码",
port = 3306,
db = "mysql",
charset = "UTF8"
)
#使用cursor()方法获取操作游标
cursor = conn.cursor()
#cursor.execute("DROP TABLE if 安居客20160917 exs")
sql= """
create table 安居客20160917(
房价 CHAR (30),
地址 CHAR (200))
"""
cursor.execute(sql)
#cursor.close()
#cursor.execute("select * from 安居客20160917")
#results = cursor.fetchall()
#print(results)
for urlOrignal in url_finally:
for i in range(1):
url = (urlOrignal["url"] + "p{}/").format(i)
data = requests.get(url).text
soup = BeautifulSoup(data, "html.parser")
address = soup.select("div.house-details > div > span.comm-address")
#print(len(address))
prices = soup.select(("div.house-details > div > span:nth-of-type(3)"))
#print(len(prices))
#break
# print(price)
#if prices == None:
#continue
#else:
for price, address in zip(prices, address):
price = price.get_text()
#print(price)
address = address.get("title")
#print(address)
try:
cursor.execute("insert into 安居客20160917 (房价,地址) values (%s,%s)",(price,address))
except:
conn.rollback()
conn.commit()
#print(url)
url = urlOrignal
cursor.close()
conn.close()
相关文章推荐
- windows和linux下使用python2.7 urllib.urlopen+beautifulsoup打开12306网站订票页面表现不同,前者报错ssl认证失败,后者成功
- Python 3.0安装BeautifulSoup插件并使用 与Python 2 version of Beautiful Soup under Python 3问题处理
- Windows下使用BeautifulSoup 出现You are trying to run the Python 2 version of Beautiful Soup under Python
- python—beautifulsoup介绍与安装使用
- python3 requests+BeautifulSoup使用多进程爬取妹子图
- Python使用Selenium+BeautifulSoup爬取淘宝搜索页
- Python BeautifulSoup基本使用
- Python爬取淘宝搜索页,使用Selenium+BeautifulSoup
- 萌新的Python学习日记 - 爬虫无影 - 使用BeautifulSoup + css selector 抓取动态网页内容:Knewone
- 萌新的Python学习日记 - 爬虫无影 - 使用BeautifulSoup + urlretrieve 抓取并保存图片:weheartit
- Python beautifulsoup的安装与使用
- 萌新的Python学习日记 - 爬虫无影 - 使用BeautifulSoup + css selector 抓取自己想要网页内容
- [Python] - 爬虫之Beautiful Soup的基本使用
- 使用python抓取并分析数据—链家网(requests+BeautifulSoup)(转)
- 使用Python+selenium+BeautifulSoup抓取动态网页的关键信息
- 使用requests+beautifulsoup模块实现python网络爬虫功能
- Python爬虫入门八之Beautiful Soup的用法
- Learn Beautiful Soup(3)——使用Beautiful Soup进行查找
- python BeautifulSoup设置页面编码的方法
- Python爬虫入门之Beautiful Soup的用法