用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. EL表达式多条件或判断用法

    简单记录一EL表达式的判断用法 <c:if test="${(order.status == '06'&& order.type=='02') || (order.st ...

  2. List of the best open source software applications

    List of the best open source software applications by Ryan • Oct 25th, 2008 • Category: Featured Art ...

  3. day3_python之函数基础知识

    一 .为何要用函数之不用函数的问题 #1.代码的组织结构不清晰,可读性差 #2.遇到重复的功能只能重复编写实现代码,代码冗余 #3.功能需要扩展时,需要找出所有实现该功能的地方修改之,无法统一管理且维 ...

  4. webstorm破解教程

    1.下载地址 官网:https://www.jetbrains.com/webstorm/ 下载好之后按照提示安装即可,这里就不再多说了.下面直接说说如何使用补丁破解. 2.使用补丁破解 (http: ...

  5. hdu 1532 Drainage Ditches(最大流模板题)

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. 2012-4-2 通过MdiParent设置窗体最前

    SentenceForm form = new SentenceForm(); form.MdiParent = this; form.Show(); //form.MdiParent = this; ...

  7. jq添加插入删除元素

    https://www.cnblogs.com/sandraryan/ append() - 在被选元素的结尾插入内容 <body> <div class="wrap&qu ...

  8. IDEA中安装activiti并使用

    1.IDEA中本身不带activiti,需要自己安装下载. 打开IDEA中File列表下的Settings 输入actiBPM,然后点击下面的Search...搜索 点击Install 下载 下载结束 ...

  9. 得到Access数据库中所有表名

    public static List<string> GetShemaTables(string db)        {                        string pa ...

  10. PHP两个变量值互换(不用第三变量)

    <?php /**  * 双方变量为数字或者字符串时  * 使用list()和array()方法可以达到交换变量值得目的  */ $a = "This is A"; // a ...