美文网首页
用BeautifulSoup和requests采集小猪短租的一个

用BeautifulSoup和requests采集小猪短租的一个

作者: 莫莫33 | 来源:发表于2016-11-08 00:25 被阅读0次

Python使用版本3.52

废话不说了,最近倍受打击,很多前面的东西看不懂了,继续努力吧,把之前的采集小猪短租的一个单页面信息的代码放上来,随后陆续更吧:

#此代码为抓取单一页面上的显示的其中一个链接的抓取元素

frombs4importBeautifulSoup

importrequests

url='http://sh.xiaozhu.com/fangzi/759632739.html'

html=requests.get(url)#发送请求网络资源

soup=BeautifulSoup(html.text,'lxml')#解析请求回来的网络资源

title=soup.select('div.pho_info > h4')[0].text#把需要爬取得信息用选择器选择出来

cate=soup.select('div.pho_info > p')[0].get('title')

price=soup.select('div.day_l > span')[0].text

pic1=soup.select('#curBigImage')[0].get('src')

pic2=soup.select('div.member_pic > a > img')[0].get('src')

gender=soup.select('div.member_pic > div')[0].get('class')[0]

name=soup.select('div.w_240 > h6 > a')[0].get('title')

score=soup.select('div.top_bar.clearfix > ul > li > em')[0].text

size=soup.select('#introduce > li.border_none > p')[0].text

defprint_gender(xb):#设定标签判断返回性别

ifxb=='member_ico1':

return'女'

else:

return'男'

data={#建立抓取元素显示标准的字典

'1.标题:':title,

'2.地址:':cate,

'3.每晚价格:':price,

'4.房图:':pic1,

'5.房主图:':pic2,

'6.房主昵称:':name,

'7.房主性别:':print_gender(gender),

'8.房子大小:':size,

'9.评分:':score,

}

forkeys,valuesindata.items():#打印单一页面上的单一链接中的抓取元素

print(keys,values)

我知道自己的水平很低,但是我一直在坚持 我的信条是“立志行动坚持”,加油吧 每一个想努力实现梦想的小伙伴们 

相关文章

网友评论

      本文标题:用BeautifulSoup和requests采集小猪短租的一个

      本文链接:https://www.haomeiwen.com/subject/jofbuttx.html