连续爬取多页数据

分析每一页url的关联找出联系

例如虎扑

第一页:https://voice.hupu.com/nba/1

第二页:https://voice.hupu.com/nba/2

第三页:https://voice.hupu.com/nba/3......

urls = ["https://voice.hupu.com/nba/{}".format(str(i)) for i in range(1, 30, 1)]
print(urls)

这样就获得了30页的url

['https://voice.hupu.com/nba/1', 'https://voice.hupu.com/nba/2', 'https://voice.hupu.com/nba/3', 'https://voice.hupu.com/nba/4', 'https://voice.hupu.com/nba/5' ......]

在做连续爬取之前还需要做一些事,防止一些网站具有反爬取机制封了你的id。

一般的操作就是让机器模仿人类的访问形式,正常机器访问动不动就是每秒成百上千次,是个人检测一下都知道你是爬虫了,所以我们让机器每隔两秒爬取一次就能模仿人类的访问规律,来达到浑水摸鱼,偷天换日啦啦啦啦啦

然后我们导入time库,在爬取过程中执行sleep函数,为了安全起见我设置成了3秒

urls = ["https://voice.hupu.com/nba/{}".format(str(i)) for i in range(1, 30, 1)]
def get_hupu(url):
   soup = BeautifulSoup(urlopen(url), 'lxml')
   time.sleep(3)
   names = soup.select("body > div.hp-wrap > div.voice-main > div.news-list > ul > li > div.list-hd > h4 > a")
   froms = soup.select("body > div.hp-wrap > div.voice-main > div.news-list > ul > li > div.otherInfo > span.other-left > span > a")
   for name, fromd in zip(names, froms):
       data = {
           "name": name.get_text(),
           "froms": fromd.get_text()
      }
       print(data)
for single_url in urls:
   get_hupu(single_url)

简单的就完成了。

练习爬取小猪网

from bs4 import BeautifulSoup
import requests
import time


headers = {
   "user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36",
   "cookie":"abtest_ABTest4SearchDate=b; gr_user_id=575e77db-5439-4516-a280-090df337f0b8; 59a81cc7d8c04307ba183d331c373ef6_gr_session_id=f9fb54b0-1b93-4224-8af5-a70f0884e2f3; 59a81cc7d8c04307ba183d331c373ef6_gr_last_sent_sid_with_cs1=f9fb54b0-1b93-4224-8af5-a70f0884e2f3; 59a81cc7d8c04307ba183d331c373ef6_gr_last_sent_cs1=N%2FA; 59a81cc7d8c04307ba183d331c373ef6_gr_session_id_f9fb54b0-1b93-4224-8af5-a70f0884e2f3=true; grwng_uid=f5b73ce1-bdb6-4d78-8f60-74319f032fe3; xzuuid=bb1b9aaa; TY_SESSION_ID=ec1f581b-5e84-4c68-8be3-845bc54f9e7e; startDate=2019-04-20; endDate=2019-04-21; xz_guid_4se=6e96f748-c8b1-44a2-9eec-be586cf5d250; haveapp=1; openappled=1"
}
urls = ["http://bj.xiaozhu.com/search-duanzufang-p{}-0/?startDate=2019-04-20&endDate=2019-04-21".format(str(i)) for i in range(1, 30, 1)]
def get_xiaozhu(url):
   web_data = requests.get(url, headers=headers)
   time.sleep(3)
   soup = BeautifulSoup(web_data.text, 'lxml')
   images = soup.select("#page_list > ul > li > a > img")
   titles = soup.select("#page_list > ul > li > div.result_btm_con.lodgeunitname > div.result_intro > a > span")
   costs = soup.select("#page_list > ul > li > div.result_btm_con.lodgeunitname > div:nth-child(1) > span > i")
   for image, title, cost in zip(images, titles, costs):
       data = {
           "image": image.get("src"),
           "title": title.get_text(),
           "cost": cost.get_text()
      }
       print(data)
for url in urls:
   get_xiaozhu(url)
{'image': '../images/lazy_loadimage.png', 'title': '立水桥5/13号线鸟巢奥森清新宜人两居室', 'cost': '488'}
{'image': '../images/lazy_loadimage.png', 'title': '凯乐公寓.4号线星光影视城温馨复式lfto', 'cost': '388'}
{'image': '../images/lazy_loadimage.png', 'title': '近北京南站角门东4号线88平米阳光大床房整租', 'cost': '599'}
{'image': '../images/lazy_loadimage.png', 'title': '凯乐公寓.4号线星光影视城温馨复式lfto', 'cost': '358'}
{'image': '../images/lazy_loadimage.png', 'title': '北京西站欢乐谷Loft温馨绿叶小屋', 'cost': '498'}
{'image': '../images/lazy_loadimage.png', 'title': '独立公寓一居亚运村鸟巢水立方国家会议中心', 'cost': '388'}
{'image': '../images/lazy_loadimage.png', 'title': '商务标准大床房', 'cost': '358'}
{'image': '../images/lazy_loadimage.png', 'title': '百子湾 ,三里屯,国贸,欢乐谷时尚浪漫小屋', 'cost': '498'}
.......

使用爬虫抓取网站异步加载数据

什么是异步加载:异步加载就是在执行过程同时加载,通常会使图片之类重要性较次的东西,可以先忽略掉,比如网页游戏经常会在玩的过程中,玩家都是黑影(未加载图形,由其他黑影模型代替),如果另一个线程完成加载了,在贴上去,就是异步。

类似新浪微博的评论系统,we heart it网站等等

如何抓取异步加载

在调试台点击Network下的XHR,这里面显示的就是网页加载ajax请求后返回的参数,通过对Request URL的分析找出规律就能异步加载数据。

练习爬取We Heart It页面的图片并保存到本地

首先保存图片到本地

def save_img(img_url,file_name):
   request.urlretrieve(img_url, file_name)

通过分析发现每个页面的url为https://weheartit.com/recent?scrolling=true&page={}里的值分别为1,2,3......这就是需要爬取的页面

然后保存图片的名称取路径名称的后4位即可区分,为了避免重复还可以扩大名称的选取

def download_weheartit(url):
   web_data = requests.get(url)
   soup = BeautifulSoup(web_data.text, 'lxml')
   images = soup.select("body > div > div > div > a > img")
   for i in images:
       file_name = "C:/Users/Y/Desktop/img_path/{}.jpg".format(i.get("src")[-4:])
       save_img(i.get("src"), file_name)
       print(i.get("src"))
download_weheartit("https://weheartit.com/recent?scrolling=true&page=1")

I am feeling good~~ ~~ ~~

The third 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 second day of Crawler learning

    用BeatuifulSoup和Requests爬取猫途鹰网 服务器与本地的交换机制 我们每次浏览网页都是再向网页所在的服务器发送一个Request,然后服务器接受到Request后返回Response ...

  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. poj 1845 【数论:逆元,二分(乘法),拓展欧几里得,费马小定理】

    POJ 1845 题意不说了,网上一大堆.此题做了一天,必须要整理一下了. 刚开始用费马小定理做,WA.(poj敢说我代码WA???)(以下代码其实都不严谨,按照数据要求A是可以等于0的,那么结果自然 ...

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

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

  3. zip解决杨辉三角问题

    杨辉三角原型: / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ 实现: a = [1] while True: print(a) ...

  4. oracle函数 NLS_INITCAP(x[,y])

    [功能]返回字符串并将字符串的第一个字母变为大写,其它字母小写; [参数]x字符型表达式 [参数]Nls_param可选, 查询数据级的NLS设置:select * from nls_database ...

  5. [***]HZOJ 优美序列

    又是一道神仙题.考试的时候居然打了一个回滚莫队,不知道我咋想的…… 先说一个某OJT80,洛谷T5分的思路(差距有点大): 可以把位置和编号映射一下,区间内最大值和最小值对应的位置,每次更新,直到找到 ...

  6. 模板—tarjan缩点

    void tarjan(int x) { dfn[x]=++cnt;low[x]=cnt; vi[x]=; stack[++top]=x; for(rint i=f(x);i;i=n(i)) if(! ...

  7. 一些关于中国剩余定理的数论题(POJ 2891/HDU 3579/HDU 1573/HDU 1930)

    2891 -- Strange Way to Express Integers import java.math.BigInteger; import java.util.Scanner; publi ...

  8. js切割字符串

    var time_str= '2019-9-10 13:18:20'; var t = time_str.substr(2,8);   console.log(t);   输出  19-9-10

  9. HTML--简单的注册网页

    利用表格,渐变色 <body background="未标题-2.jpg"> <table align="center" border=&qu ...

  10. 基于BERT预训练的中文命名实体识别TensorFlow实现

    BERT-BiLSMT-CRF-NERTensorflow solution of NER task Using BiLSTM-CRF model with Google BERT Fine-tuni ...