python - 爬虫爬https站点怎么处理的?
大家讲道理
大家讲道理 2017-04-17 17:16:00
[Python讨论组]

一段模拟登入知乎的问题
PS:这里不讨论验证码问题,我的账号登入没有提示验证码

代码信息:

# /usr/bin/python
#coding:utf-8
__author__ = 'eyu Fanne'

import requests,time
from bs4 import BeautifulSoup

begintime = time.time()



headers={
    "Host":"www.zhihu.com",
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36",
    "Accept":"*/*",
    "Accept-Language":"zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
    "Accept-Encoding":"gzip, deflate",
    "Content-Type":"application/x-www-form-urlencoded; charset=UTF-8",
    "X-Requested-With":"XMLHttpRequest",
    "Connection":"keep-alive"
}


login_url=r'https://www.zhihu.com/login/email'

html_txt = requests.get(login_url,verify=False).text
html_soup = BeautifulSoup(html_txt,'lxml')
xsrf = html_soup.find("input",{"name":"_xsrf"})['value']

#print xsrf
#print html_txt
s = requests.session()

url_data={
    "_xsrf":xsrf,
    "email":"******",
    "password":"********",
    'remember_me':'true'
}

s_login=s.post(login_url,data=url_data,headers=headers,verify=False)
print s_login.text

endtime = time.time()
usetime =  endtime - begintime
print "执行脚本总用时 %s 秒" % usetime


#print s.get('https://www.zhihu.com',verify=False).text

执行结果:

C:\Python27_x86\lib\site-packages\requests\packages\urllib3\connectionpool.py:768: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
  InsecureRequestWarning)
C:\Python27_x86\lib\site-packages\requests\packages\urllib3\connectionpool.py:768: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
  InsecureRequestWarning)
C:\Python27_x86\lib\site-packages\requests\packages\urllib3\connectionpool.py:768: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
  InsecureRequestWarning)
{"r":0,
 "msg": "\u767b\u9646\u6210\u529f"
}
执行脚本总用时 73.2650001049 秒

在模拟登入的时候,不知道是不是因为https的问题处理的好慢,整个模拟登入要73s才完成,不知卡在哪里了。
像类似这种问题的应该怎么处理。

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(2)
PHP中文网
from requests.packages.urllib3.exceptions import InsecureRequestWarning,InsecurePlatformWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
requests.packages.urllib3.disable_warnings(InsecurePlatformWarning)

可以解决告警问题,以及解决速度问题

PHP中文网

可以用cProfile模块,先把所有过程封装到一个函数里,然后cProfile看看哪个函数调用的时间最长。

不过你这程序在我这里运行起来挺快的,应该和你的网络环境有关。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号