V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Recommended Services
Amazon Web Services
LeanCloud
New Relic
ClearDB
GrahamCloud
V2EX  ›  云计算

带你玩一个练手的项目

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

    今天我们一起试着八一八知乎日报,当然是跟着萌萌哒 造数 君一起 ^_^

    文章最早发布于知乎,反响不错分享给大家,原链接在这里

    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>

    500 Server Error

    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 print(len(res)) 我们把"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',))) 3、模拟登录 有些网站是需要登录才能看到信息的,例如知乎,直接用 requests 获取知乎首页信息,返回数据是需要你登录的,只有登录了才能看到数据。

    无法登录?

    社交帐号登录
    再次回到官方文档 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 来解析网页,获取我们想要的数据。

    不想自己写?还是来造数!!

    xgfan
        1
    xgfan  
       2017-05-31 11:42:40 +08:00   ❤️ 1
    复制粘贴之后都不预览一下?
    KIDJourney
        2
    KIDJourney  
       2017-05-31 11:44:50 +08:00
    复制的好丑。
    justtery
        3
    justtery  
       2017-05-31 12:04:07 +08:00 via Android
    这格式。。让我看个啥,一点耐心都没有了
    justfun
        4
    justfun  
       2017-05-31 12:55:53 +08:00 via iPhone   ❤️ 1
    Shit
    tausi0661
        5
    tausi0661  
       2017-05-31 13:30:11 +08:00
    这排版, 不是造数, 是造孽
    GrahamCloud
        6
    GrahamCloud  
    OP
       2017-05-31 13:32:03 +08:00
    @tausi0661 重新排版上线了,刚才太疏忽了
    GrahamCloud
        7
    GrahamCloud  
    OP
       2017-05-31 13:32:42 +08:00
    @xgfan @KIDJourney
    @justtery
    @justfun 感谢,重新排版给大家看。
    ddter
        8
    ddter  
       2017-05-31 13:40:42 +08:00   ❤️ 1
    这广告打的给负分
    GrahamCloud
        9
    GrahamCloud  
    OP
       2017-05-31 13:51:54 +08:00
    @ddter 重新排版的已经置顶,本帖争取做下沉处理
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3254 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 14:14 · PVG 22:14 · LAX 07:14 · JFK 10:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.