前言
在定位房屋名称,如下图所示,位于
节点下。
接下来我们写个简单的BeautifulSoup进行爬取。
文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。
作者: Eastmount
PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取
python免费学习资料以及群交流解答点击即可加入
我们在编写Python爬虫时,有时会遇到网站拒绝访问等反爬手段,比如这么我们想爬取蚂蚁短租数据,它则会提示“当前访问疑似黑客攻击,已被网站管理员设置为拦截”提示,如下图所示。此时我们需要采用设置Cookie来进行爬取,下面我们进行详细介绍。非常感谢我的学生承峰提供的思想,后浪推前浪啊!
一. 网站分析与爬虫拦截当我们打开蚂蚁短租搜索贵阳市,反馈如下图所示结果。 我们可以看到短租房信息呈现一定规律分布,如下图所示,这也是我们要爬取的信息。
通过浏览器审查元素,我们可以看到需要爬取每条租房信息都位于


# -*- coding: utf-8 -*-
import urllib
import re
from bs4 import BeautifulSoup
import codecs
url = 'http://www.mayi.com/guiyang/?map=no'
response=urllib.urlopen(url)
contents = response.read()
soup = BeautifulSoup(contents, "html.parser")
print soup.title
print soup
#短租房名称
for tag in soup.find_all('dd'):
for name in tag.find_all(attrs={"class":"room-detail clearfloat"}):
fname = name.find('p').get_text()
print u'[短租房名称]', fname.replace('\n','').strip()
但很遗憾,报错了,说明蚂蚁金服防范措施还是挺到位的。
添加消息头的代码如下所示,这里先给出代码和结果,再教大家如何获取Cookie。
# -*- coding: utf-8 -*-
import urllib2
import re
from bs4 import BeautifulSoup
#爬虫函数
def gydzf(url):
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
headers={"User-Agent":user_agent}
request=urllib2.Request(url,headers=headers)
response=urllib2.urlopen(request)
contents = response.read()
soup = BeautifulSoup(contents, "html.parser")
for tag in soup.find_all('dd'):
#短租房名称
for name in tag.find_all(attrs={"class":"room-detail clearfloat"}):
fname = name.find('p').get_text()
print u'[短租房名称]', fname.replace('\n','').strip()
#短租房价格
for price in tag.find_all(attrs={"class":"moy-b"}):
string = price.find('p').get_text()
fprice = re.sub("[¥]+".decode("utf8"), "".decode("utf8"),string)
fprice = fprice[0:5]
print u'[短租房价格]', fprice.replace('\n','').strip()
#评分及评论人数
for score in name.find('ul'):
fscore = name.find('ul').get_text()
print u'[短租房评分/评论/居住人数]', fscore.replace('\n','').strip()
#网页链接url
url_dzf = tag.find(attrs={"target":"_blank"})
urls = url_dzf.attrs['href']
print u'[网页链接]', urls.replace('\n','').strip()
urlss = 'http://www.mayi.com' + urls + ''
print urlss
#主函数
if __name__ == '__main__':
i = 1
while i
关注
打赏
热门博文
- Python骚操作,实现驾考自动答题,这就直接满分了?
- 用Python自动实现图表可视化操作,提高工作效率,又能有更多的时间摸鱼了~
- Python:用tkinter制做一个音乐下载小软件
- Python丨小学妹喜欢看漫画,于是我写了四十行代码获取了它所有漫画
- 女同桌找我要表情包,还好我会Python,分分钟给她下载几十个G...
- 为了防止这上面的文章被封,我连夜用Python获取了它所有内容,真香~
- 这个Python读取文件的方法,堪称天花板级别...
- Python做一个通过输入bv号就能下载视频的工具,评论和弹幕也不放过
- Python爬虫何如抓包?这三个案例手把手教会你,非常详细...
- Python:50行代码实现下载小说,图片章节可自动识别转文字保存...