python3 [爬虫实战] selenium + requests 爬取安居客
来源:互联网 发布:plc用c语言编程 编辑:程序博客网 时间:2024/05/07 19:06
很简单,这里是根据网友的求助爬取的安居客上的一个页面的全部地区名称跟链接
因为她用的scrapy框架,感觉有些大才小用了,所以就直接用了一个requests库,selenium 和xpath进行一整页数据的爬取
我们爬取的网站:https://www.anjuke.com/sy-city.html
获取的内容:包括地区名,地区链接:
1 一开始直接用requests库进行网站的爬取,会访问不到数据的, 会直接出现 访问的页面出现错误的信息。(ps:这里就暂时不打印出来了。)
2 因为一直报错,脑瓜子不知道怎么的就想到了selenium 这个框架,可能是爬安居客之前用selenium 爬取了天猫的商品内容吧。
3 selenium 的使用,我的博客上有说过:
http:/blog.csdn.net/xudailong_blog/
4 现在贴上代码片段:
# -*- coding: utf-8 -*-# @Time : 2017/9/19 21:36# @Author : 蛇崽# @Email : 17193337679@163.com# @File : anjuke.py 安居客房产网import requestsimport refrom bs4 import BeautifulSoupimport csvimport timeimport threadingfrom lxml import etreefrom selenium import webdriverfrom openpyxl import Workbooknum0 = 1 # 用来计数,计算爬取的书一共有多少本baseurl = 'https://www.anjuke.com/sy-city.html'wb = Workbook()ws = wb.activews.title = '安居客'ws.cell(row=1, column=1).value = '城市名称'ws.cell(row=1, column=2).value = '城市链接'def gethtml(): chromedriver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe" browser = webdriver.Chrome(chromedriver) browser.get(baseurl) time.sleep(5) js = 'window.scrollBy(0,3000)' browser.execute_script(js) js = 'window.scrollBy(0,5000)' browser.execute_script(js) html = browser.page_source return htmldef saveinfos(authorother): global num0 nums = 0 for ver_info in authorother: num0 = num0 + 1 ws.cell(row=num0, column=1).value = ver_info[0] ws.cell(row=num0, column=2).value = ver_info[1] nums += 1 print('爬取成功 ' + str(nums)) wb.save('安居客' + '.xlsx') passdef parseHotBook(html): # 作者 (豆瓣用户,简书) print(html) print('*'*20) # commentlist = html.xpath("/html/body/div[3]/div") # 作者 (豆瓣用户,简书) regAuthor = r'.*?<a href="(.*?)</a>' reg_author = re.compile(regAuthor) authorother = re.findall(reg_author, html) global num0 nums = 0 for info in authorother: verinfo = info.split('">') print(verinfo[0],verinfo[1].replace('class="hot','')) num0 = num0 + 1 name = verinfo[0] link = verinfo[1].replace('class="hot','') ws.cell(row=num0, column=1).value = name ws.cell(row=num0, column=2).value = link wb.save('安居客2' + '.xlsx') print('爬取成功')html = gethtml()parseHotBook(html)
当然,文本存储还有一些瑕疵,因为用的是正则表达式,并没有进行很严格的匹配
贴上爬取图片:
正确的数据 650条左右,因为问了一下给需求的小姐姐,说是可以,所以就这样子处理了。
代码就是上面那些,以后有同样入门的一块学习的小伙伴或者需要帮忙爬虫的,可以私信我,我可以试着去爬一下,因为自己也是自学3个月左右。代码有放到GitHub上了
阅读全文
0 0
- python3 [爬虫实战] selenium + requests 爬取安居客
- python3 [爬虫实战] selenium + requests 爬取安居客
- python3[爬虫实战] 爬虫之requests爬取新浪微博京东客服
- python3 [爬虫入门实战] 爬虫之使用selenium 爬取百度招聘内容并存mongodb
- Python3网络爬虫:requests爬取动态网页内容
- [实战演练]python3使用requests模块爬取页面内容
- [实战演练]python3使用requests模块爬取页面内容
- 安居客信息爬取
- Python3.X 爬虫实战(并发爬取)
- python3[爬虫基础入门实战] 爬取豆瓣电影排行top250
- Python3.X 爬虫实战(动态页面爬取解析)
- Python3.X 爬虫实战(并发爬取)
- python3 [爬虫入门实战]爬取熊猫直播用户信息
- python3--爬虫实战一:爬取豆瓣电影250
- python3爬虫实战(一)爬取创业邦创投库
- [python3]爬虫实战一之爬取糗事百科段子
- php爬取安居客小区数据
- Python爬取安居客新房信息
- 当卷积层后跟batch normalization层时为什么不要偏置b
- 汇编语言之指令与寄存器
- 机器学习学习笔记.day10
- 第三篇:C语言的输入输出函数
- 欢迎使用CSDN-markdown编辑器
- python3 [爬虫实战] selenium + requests 爬取安居客
- SAS中数据输入和输出的几种方式
- 使用Visio 2010 画数据库中表与表的关系
- 大数据 (十九)Hive【Hive安装配置(本地数据库模式)】
- Pam x86_64 and i686 have conflicting man pages
- 第四篇:指针的强化理解
- vbox启动提示 intel_rapl: no valid rapl domains found in package 0
- 第五篇:指针和数组之间的关系
- LeetCode[561]Array Partition I