V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
GrahamCloud
V2EX  ›  推广

带大家玩一个练手的数据采集(简洁版)

  •  1
     
  •   GrahamCloud · 2017-05-31 13:30:39 +08:00 · 6229 次点击
    这是一个创建于 2493 天前的主题,其中的信息可能已经有所发展或是发生改变。

    今天由无需编程的数据采集工具 造数 带大家 扒一扒 知乎日报,当成一个有趣的练手项目

    1、获取源码

    import requests
    
    url = 'http://daily.zhihu.com/'
    
    res = requests.get(url).text
    
    print(res)
    

    个人喜欢 requests,直接访问,发现返回 500 错误

    C:\Python35\python.exe F:/PyCharm/爬虫 /daily.py
    <html><body><h1>500 Server Error</h1>
    An internal server error occured.
    </body></html>
    
    
    Process finished with exit code 0
    

    根据经验判断,是知乎禁止爬虫,需要加上一些伪装,让我们看看加上浏览器伪装效果

    import requests
    
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
    
    url = 'http://daily.zhihu.com/'
    
    res = requests.get(url,headers=headers).text
    
    print(res)
    

    看看结果,已经返回我们需要的数据 C:\Python35\python.exe F:/PyCharm/爬虫 /daily.py

    <html lang="zh-CN"><head><title>知乎日报 - 每天 3 次,每次 7 分钟</title><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><meta name="description" content="在中国,资讯类移动应用的人均阅读时长是 5 分钟,而在知乎日报,这个数字是 21。以独有的方式为你提供最高质、最深度、最有收获的阅读体验。"><link rel="stylesheet" href="/css/base.auto.css"><link rel="stylesheet" href="/css/new_home_v3.auto.css"><script src="/js/jquery.1.9.1.js"></script><script src="/js/new_index_v3/home.js"></script><link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"><base target="_blank"><style>h1,h2,h3 {padding: 0;margin:0}</style><base target="_blank"></head><body class="home">

    ...

    但是这种写法是否可以应用到所有的网站?

    2、代理设置 有时候同一个 IP 去爬取同一网站上的内容,久了之后就会被该网站服务器屏蔽。解决方法就是更换 IP。这个时候,在对方网站上,显示的不是我们真实地 IP 地址,而是代理服务器的 IP 地址。 http://www.xicidaili.com/nn/ 西刺代理提供了很多可用的国内 IP,可以直接拿来使用。

    那么如何在爬虫里加入代理呢,看看 requests 的官方文档怎么说。http://docs.python-requests.org/zh_CN/latest/user/advanced.html#proxies 如果需要使用代理,你可以通过为任意请求方法提供 proxies 参数来配置单个请求:

    import requests
    
    proxies = {
    "http": "http://10.10.1.10:3128",
    "https": "http://10.10.1.10:1080",
    }
    
    requests.get("http://example.org", proxies=proxies)
    

    用法很简单,加入 proxies 参数即可 import requests

    proxies = {
    "http": "http://121.201.24.248:8088",
    "https": "http://36.249.194.52:8118",
    }
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 	(KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
    
    url = 'http://daily.zhihu.com/'
    
    res = requests.get(url,headers=headers,proxies=proxies).text
    
    print(len(res))
    

    为了便于测试,只打印出返回数据的长度

    C:\Python35\python.exe F:/PyCharm/爬虫 /daily.py
    10830
    
    Process finished with exit code 0
    

    发现代理服务器成功爬取知乎日报的信息,内容是 10830,故意把代理 IP 写错一位数,看看结果

    import requests
    
    proxies = {
    "http": "http://121.201.24.248:8088",
    "https": "http://36.249.194.52: 222",
    }
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 	(KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
    
    url = 'http://daily.zhihu.com/'
    
    res = requests.get(url,headers=headers,proxies=proxies).text
    

    我们把"https": "http://36.249.194.52:8118"修改为"https": "http://36.249.194.52: 222",此时返回的结果如下,发现不能获取网页数据。所以,在使用代理服务器爬去网站时,如果出现异常,要考虑代理 IP 是否失效了。 当然你也可以写一个爬虫,实时抓取最新的代理 IP 用来爬取。

    Traceback (most recent call last):
    
    File "F:/PyCharm/爬虫 /daily.py", line 9, in <module>
    
    res = requests.get(url,headers=headers,proxies=proxies).text
    
    File "C:\Python35\lib\site-packages\requests\api.py", line 70, in get
    
    return request('get', url, params=params, **kwargs)
    
    File "C:\Python35\lib\site-packages\requests\api.py", line 56, in request
    
    return session.request(method=method, url=url, **kwargs)
    
    File "C:\Python35\lib\site-packages\requests\sessions.py", line 488, in request
    
    resp = self.send(prep, **send_kwargs)
    
    File "C:\Python35\lib\site-packages\requests\sessions.py", line 609, in send
    
    r = adapter.send(request, **kwargs)
    
    File "C:\Python35\lib\site-packages\requests\adapters.py", line 485, in send
    
    raise ProxyError(e, request=request)
    
    requests.exceptions.ProxyError: HTTPConnectionPool(host='121.201.24.248:8088', port=80): Max retries exceeded with url: http://daily.zhihu.com/ (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x0000000003860DA0>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed',)))
    
    print(len(res))
    

    3、模拟登录 有些网站是需要登录才能看到信息的,例如知乎,直接用 requests 获取知乎首页信息,返回数据是需要你登录的,只有登录了才能看到数据。

    <button type="button" class="signin-switch-button">手机验证码登录</button>
    
    <a class="unable-login" href="#">无法登录?</a>
    </div>
    
    
    <div class="social-signup-wrapper" data-za-module="SNSSignIn">
    
    <span class="name js-toggle-sns-buttons">社交帐号登录</span>
    
    <div class="sns-buttons">
    
    <a title="微信登录" class="js-bindwechat" href="#"><i class="sprite-index-icon-				wechat"></i></a>
    
    <a title="微博登录" class="js-bindweibo" href="#"><i class="sprite-index-icon-				weibo"></i></a>
    
    <a title="QQ 登录" class="js-bindqq" href="#"><i class="sprite-index-icon-qq"></i></a>
    </div>
    

    再次回到官方文档 http://docs.python-requests.org/zh_CN/latest/user/quickstart.html#cookie

    如果某个响应中包含一些 cookie,你可以快速访问它们:

    url = 'http://example.com/some/cookie/setting/url' r = requests.get(url)

    r.cookies['example_cookie_name'] 'example_cookie_value' 要想发送你的 cookies 到服务器,可以使用 cookies 参数:

    url = 'http://httpbin.org/cookies' cookies = dict(cookies_are='working')

    r = requests.get(url, cookies=cookies) r.text '{"cookies": {"cookies_are": "working"}}'

    具体的分析过程可以参考 xchaoinfo 所写的文章和视频,讲解十分清晰 https://zhuanlan.zhihu.com/p/25633789 下面是代码

    import requests
    from bs4 import BeautifulSoup
    import os, time
    import re
    

    import http.cookiejar as cookielib

    构造 Request headers

    agent = 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Mobile Safari/537.36'
    
    headers = {
    
    "Host": "www.zhihu.com",
    
    "Referer": "https://www.zhihu.com/",
    
    'User-Agent': agent
    

    }

    ######### 构造用于网络请求的 session session = requests.Session()

    session.cookies = cookielib.LWPCookieJar(filename='zhihucookie')

    try:

    session.cookies.load(ignore_discard=True)

    except:

    print('cookie 文件未能加载')

    ############ 获取 xsrf_token

    homeurl = 'https://www.zhihu.com'
    homeresponse = session.get(url=homeurl, headers=headers)
    homesoup = BeautifulSoup(homeresponse.text, 'html.parser')
    xsrfinput = homesoup.find('input', {'name': '_xsrf'})
    xsrf_token = xsrfinput['value']
    print("获取到的 xsrf_token 为: ", xsrf_token)
    

    ########## 获取验证码文件

    randomtime = str(int(time.time() * 1000))
    captchaurl = 'https://www.zhihu.com/captcha.gif?r='+\
             randomtime+"&type=login"
    
    captcharesponse = session.get(url=captchaurl, headers=headers)
    with open('checkcode.gif', 'wb') as f:
    
    f.write(captcharesponse.content)
    
    f.close()
    

    os.startfile('checkcode.gif')

    captcha = input('请输入验证码:')
    
    print(captcha)
    

    ########### 开始登陆

    headers['X-Xsrftoken'] = xsrf_token
    
    headers['X-Requested-With'] = 'XMLHttpRequest'
    
    loginurl = 'https://www.zhihu.com/login/email'
    
    postdata = {
    '_xsrf': xsrf_token,
    'email': '邮箱 @qq.com',
    'password': '密码'
    

    }

    loginresponse = session.post(url=loginurl, headers=headers, data=postdata)
    print('服务器端返回响应码:', loginresponse.status_code)
    print(loginresponse.json())
    

    验证码问题输入导致失败: 猜测这个问题是由于 session 中对于验证码的请求过期导致

    if loginresponse.json()['r']==1:
    

    重新输入验证码,再次运行代码则正常。也就是说可以再第一次不输入验证码,或者输入一个错误的验证码,只有第二次才是有效的

    randomtime = str(int(time.time() * 1000))
    
    captchaurl = 'https://www.zhihu.com/captcha.gif?r=' + \
                 randomtime + "&type=login"
    captcharesponse = session.get(url=captchaurl, headers=headers)
    
    with open('checkcode.gif', 'wb') as f:
        f.write(captcharesponse.content)
        f.close()
    
    os.startfile('checkcode.gif')
    
    captcha = input('请输入验证码:')
    
    print(captcha)
    
    postdata['captcha'] = captcha
    
    loginresponse = session.post(url=loginurl, headers=headers, data=postdata)
    
    print('服务器端返回响应码:', loginresponse.status_code)
    
    print(loginresponse.json())
    

    ##########################保存登陆后的 cookie 信息

    session.cookies.save()

    ############################判断是否登录成功

    profileurl = 'https://www.zhihu.com/settings/profile'
    
    profileresponse = session.get(url=profileurl, headers=headers)
    
    print('profile 页面响应码:', profileresponse.status_code)
    
    profilesoup = BeautifulSoup(profileresponse.text, 'html.parser')
    
    div = profilesoup.find('div', {'id': 'rename-section'})
    
    print(div)	
    

    好了关于爬虫的第一步,获取源码这一节讲了很多,其实大多数网站加上 User-Agent 和代理 IP 就可以正常爬取。下一节会讲讲如何利用 xpath 来解析网页,获取我们想要的数据。

    不用编程的简单采集数据 就来 造数 云爬虫!

    16 条回复    2017-06-01 14:18:12 +08:00
    bozong
        1
    bozong  
       2017-05-31 13:59:26 +08:00   ❤️ 6
    。。。。。。
    GrahamCloud
        2
    GrahamCloud  
    OP
       2017-05-31 14:06:31 +08:00
    @bozong 求意见
    solomaster
        3
    solomaster  
       2017-05-31 15:04:49 +08:00
    代理 ip 部分能不能每轮请求自动随机切换 ip ?这里是一直使用同一个 ip,失效了才手动切换的吧。
    HGladIator
        4
    HGladIator  
       2017-05-31 15:50:58 +08:00   ❤️ 1
    1. 排版乱
    2. 前后链接造数 不是说不用编程么 这和文章全篇没关系的啊
    GrahamCloud
        5
    GrahamCloud  
    OP
       2017-05-31 17:34:35 +08:00
    @HGladIator 话虽这样说,强力干货也是很想多输出一下。
    scriptB0y
        6
    scriptB0y  
       2017-05-31 18:02:21 +08:00   ❤️ 1
    这看起来是一篇在什么地方发布之后大量复制粘贴到别的社区但是连排版都懒得改一下读起来很难受是的就像你现在读的这一句话一样的文章。
    eminemcola
        7
    eminemcola  
       2017-05-31 18:28:35 +08:00   ❤️ 1
    阅读体验不好不仅仅是排版不好。建议换个角度重新读一下,你会发现这是一篇写给自己看的类似笔记的东西,而不是一篇用来输出给别人看的技术博文。
    janezhang
        8
    janezhang  
       2017-05-31 19:29:43 +08:00
    还记得当年非常小白的用网络旷工爬数据,哎.....。
    xiaoyu9527
        9
    xiaoyu9527  
       2017-05-31 19:42:46 +08:00
    @janezhang 我感觉现在可视化多了以后。其实爬这个的动作已经很简单的。 即便代理 IP 什么的 火车都有对应的接口。

    难的是后续维护。
    mingyun
        10
    mingyun  
       2017-05-31 22:47:30 +08:00   ❤️ 1
    这排版没法看
    GrahamCloud
        11
    GrahamCloud  
    OP
       2017-06-01 13:12:06 +08:00
    @scriptB0y 排版改过一次了,回头再改下吧
    WildCat
        12
    WildCat  
       2017-06-01 13:18:02 +08:00   ❤️ 1
    block
    GrahamCloud
        13
    GrahamCloud  
    OP
       2017-06-01 13:31:50 +08:00
    @mingyun 下次上个更好的
    GrahamCloud
        14
    GrahamCloud  
    OP
       2017-06-01 13:32:16 +08:00
    @eminemcola 收到
    GrahamCloud
        15
    GrahamCloud  
    OP
       2017-06-01 13:32:40 +08:00
    @WildCat 感谢意见
    kokdemo
        16
    kokdemo  
       2017-06-01 14:18:12 +08:00
    v2ex 一般是链接转载,这种发全文的不太受欢迎
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1710 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 16:43 · PVG 00:43 · LAX 09:43 · JFK 12:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.