用BeatuifulSoup和Requests爬取猫途鹰网

服务器与本地的交换机制

我们每次浏览网页都是再向网页所在的服务器发送一个Request,然后服务器接受到Request后返回Response给网页。

Request

当前Http1.1版本共有get、post、head、put、options、connect、trace、delete共八种发送请求的方式。不过不需要全部记住,目前最常用的为get和post。

Response

我们会在Response中得到服务器返回给我们的信息,例如status_code为304或200则表示访问网页成功。

可能会发生的问题:

403问题:在我用urlopen打开TripAdvisor准备访问页面时,我的请求没有设置其他参数,该网页的服务器获取不到我发送请求的浏览器,操作系统等等一些信息,服务器会将这次访问视为非正常访问,这是很多网站都具有的反爬虫机制。

解决方法:

url = "https://www.tripadvisor.cn/"
wb_data = requests.get(url)
Soup = BeautifulSoup(wb_data.text, "lxml")

爬取猫途鹰网

爬取基本信息
# 爬取猫途鹰
url = "https://www.tripadvisor.cn/"
wb_data = requests.get(url)
Soup = BeautifulSoup(wb_data.text, "lxml")
# Soup = BeautifulSoup(urlopen("https://www.tripadvisor.cn/"), "lxml")
images = Soup.select("#popularDestinations > div.section > ul.regionContent > li.active > ul > li > a > span.thumbCrop > img")
nations = Soup.select("#popularDestinations > div.section > ul.regionContent > li.active > ul > li > div.title > a.countryName")
locations = Soup.select("#popularDestinations > div.section > ul.regionContent > li.active > ul > li > div.title > a.cityName")
messages = Soup.select("#popularDestinations > div.section > ul.regionContent > li.active > ul > li > div.counts > span.attractionCount > a")
tags = Soup.select("div.popIcons")

Info = []
for image, nation, location, message, tag in zip(images, nations, locations, messages, tags):
   data = {
       "image": image.get("src"),
       "nation": nation.get_text(),
       "location": location.get_text(),
       "message": message.get_text(),
       "tag": list(tag.stripped_strings)
  }
   Info.append(data)
   print(data)
{'image': 'https://ccm.ddcdn.com/ext/photo-s/03/9b/30/02/phuket.jpg', 'nation': '泰国', 'location': '普吉岛', 'message': '景点 1917', 'tag': ['游记', '指南']}
{'image': 'https://ccm.ddcdn.com/ext/photo-s/03/9b/2d/ad/bangkok.jpg', 'nation': '泰国', 'location': '曼谷', 'message': '景点 2700', 'tag': ['游记', '指南']}
{'image': 'https://ccm.ddcdn.com/ext/photo-b/1280x250/03/9b/2d/c0/chiang-mai.jpg', 'nation': '泰国', 'location': '清迈', 'message': '景点 1337', 'tag': ['游记', '指南']}
.......
伪造Cookie爬取需要登陆才能获取到的信息

暂时不知道怎么用,无论加不加都能爬取的到。。。。。。。。。。先记录一下

https://www.tripadvisor.cn/TravelMapHome为例子

构造向服务器提交的参数:headers

这些数据在你已登陆的页面的Request headers下都能找到,我们便是仿照这个Cookie告诉服务器我们已经登陆,以此跳过登陆环节,直接爬取。

爬取赛欢网

目标解析赛欢网的各种比赛的分类,并组成三层一对多的列表。

获取第一层主分类(topType)
tops = soup.select("#wp > div > div.w1180 > div > ul > li > h5 > span > a")
for top in tops:
   data = {
       "top" : top.get_text()
  }
   print(data)
{'top': '基础学科类'}{'top': '创业商业类'}{'top': '科技创新类'}{'top': '数学建模类'}{'top': '平面艺术类'}{'top': '游戏动漫类'}{'top': '志愿活动类'}{'top': '地区竞赛类'}
获取第二层分类(middleType)

由于第一层分类和第二层分类之间具有一对多的关系,我们需要找到一片区域将一对多中的多打包起来,例如科技创新类下有多个二层分类(都在class为nexinnercontents的div下),我们便可以获取class为portal_block_summary的div(里面包含多个二层分类)将其打包。

url = "https://www.saihuan.net/"
soup = BeautifulSoup(urlopen(url), "lxml")
tops = soup.select("#wp > div > div.w1180 > div > ul > li > h5 > span > a")
middles = soup.select("div.portal_block_summary")
for top, middle in zip(tops, middles):
   data = {
       "top": top.get_text(),
       "middle": list(middle.stripped_strings)
  }
   print(data)
{'top': '基础学科类', 'middle': ['基础学科', '数学', '物理', '化学', '化工', '力学', '地理', '医学']}
{'top': '创业商业类', 'middle': ['创业商业', '创业', '商业', '电商', '互联网+', '策划营销', '策划', '销售', '营销', '市场', '案例']}
......

这时候第二层分类虽然已经打包进来了,但是发现第三层的分类也被打包进来了,原因在于第三层分类都在class为nexinnertxts的div中,也在刚刚的打包范围内。

The second day of Crawler learning的更多相关文章

  1. The sixth day of Crawler learning

    爬取我爱竞赛网的大量数据 首先获取每一种比赛信息的分类链接 def get_type_url(url):    web_data = requests.get(web_url)    soup = B ...

  2. The fifth day of Crawler learning

    使用mongoDB 下载地址:https://www.mongodb.com/dr/fastdl.mongodb.org/win32/mongodb-win32-x86_64-2008plus-ssl ...

  3. The fourth day of Crawler learning

    爬取58同城 from bs4 import BeautifulSoupimport requestsurl = "https://qd.58.com/diannao/35200617992 ...

  4. The third day of Crawler learning

    连续爬取多页数据 分析每一页url的关联找出联系 例如虎扑 第一页:https://voice.hupu.com/nba/1 第二页:https://voice.hupu.com/nba/2 第三页: ...

  5. The first day of Crawler learning

    使用BeautifulSoup解析网页 Soup = BeautifulSoup(urlopen(html),'lxml') Soup为汤,html为食材,lxml为菜谱 from bs4 impor ...

  6. Machine and Deep Learning with Python

    Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...

  7. Node.js Learning Paths

    Node.js Learning Paths Node.js in Action Node.js Expert situations / scenario Restful API OAuth 2.0 ...

  8. 【Machine Learning】KNN算法虹膜图片识别

    K-近邻算法虹膜图片识别实战 作者:白宁超 2017年1月3日18:26:33 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本系列文章是作者结 ...

  9. 【Machine Learning】Python开发工具:Anaconda+Sublime

    Python开发工具:Anaconda+Sublime 作者:白宁超 2016年12月23日21:24:51 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现 ...

随机推荐

  1. iOS Animation 主流炫酷动画框架(特效)收集整理 #91

    https://github.com/sxyx2008/DevArticles/issues/91

  2. 【vb.net机房收费系统】之sqlhelper 标签: 数据库 2015-05-17 10:47 819人阅读 评论(15)

    在敲机房收费重构版的时候,用到了sqlhelper,当时不知道怎么开始,各种听别人说,张晗说,一定要用sqlhelper,特别好用,我当时没有用balabala~当时一听,哎哎哎,这个高级,要搞一搞, ...

  3. @codechef - SONATR@ Sonya and Tree

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定 p 为 0~N-1 的一个排列,并给定一棵 N 个点的树. ...

  4. 【SDOI2015】bzoj3990 排序

    A. 排序 题目描述 输入格式 输出格式 一行,一个整数,表示可以将数组A从小到大排序的不同的操作序列的个数. 样例 样例输入 3 7 8 5 6 1 2 4 3 样例输出 6 数据范围与提示 对于3 ...

  5. Go 语言开发工具

    Go 语言开发工具 LiteIDE LiteIDE是一款开源.跨平台的轻量级Go语言集成开发环境(IDE). 支持的操作系统 Windows x86 (32-bit or 64-bit) Linux ...

  6. H3C IP及其相关协议

  7. sleep usleep nanosleep alarm setitimer使用

    sleep使用的是alarm之类的定时器,定时器是使得进程被挂起,使进程处于就绪的状态. signal+alarm定时器 alarm参数的类型为uint, 并且不能填0 #include <st ...

  8. Laravel 修改默认日志文件名称和位置

    修改默认日志位置 我们平常的开发中可能一直把laravel的日志文件放在默认位置不会有什么影响,但如果我们的项目上线时是全量部署,每次部署都是git中最新的代码,那这个时候每次都会清空我们的日志,显示 ...

  9. css技巧 1200px居中容器中某个div增加横屏背景

    <div class='container' style='width:1200px;margin:0 auto;'> <div style='width:200px;margin: ...

  10. const(每个对象中的常量), static const(类的编译时常量)

    1 每个对象中的常量 --- const数据成员 const限定,意味着“在该对象生命周期内,它是一个常量”. 关键字const 使被限定的量为常量 在该类的每个对象中,编译器都为其const数据成员 ...