第五章感觉是第四章的练习项目,无非就是多了一个模拟登录。

不分小节记录了,直接上知识点,可能比较乱。

1.常见的httpcode:

2.怎么找post参数?

先找到登录的页面,打开firebug,输入错误的账号和密码,观察post_url变换,从而确定参数。

3.读取本地的文件,生成cookies。

 try:
import cookielib #py2
except:
import http.cookiejar as cookielib #py3

4.用requests登录知乎

 # -*- coding: utf-8 -*-
__author__ = 'jinxiao' import requests
try:
import cookielib
except:
import http.cookiejar as cookielib import re session = requests.session() #实例化session,下面的requests可以直接换成session
session.cookies = cookielib.LWPCookieJar(filename="cookies.txt") #实例化cookies,保存cookies
#读取cookies
try:
session.cookies.load(ignore_discard=True)
except:
print ("cookie未能加载") #知乎一定要加上浏览器的头,其他网站不一定,一般都是要的
agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0"
header = {
"HOST":"www.zhihu.com",
"Referer": "https://www.zhizhu.com",
'User-Agent': agent
} def is_login():
#通过个人中心页面返回状态码来判断是否为登录状态
inbox_url = "https://www.zhihu.com/question/56250357/answer/148534773"
response = session.get(inbox_url, headers=header, allow_redirects=False) #禁止重定向,判断为是否登录
if response.status_code != 200:
return False
else:
return True def get_xsrf():
#获取xsrf code
response = session.get("https://www.zhihu.com", headers=header)
match_obj = re.match('.*name="_xsrf" value="(.*?)"', response.text)
if match_obj:
return (match_obj.group(1))
else:
return "" def get_index():
response = session.get("https://www.zhihu.com", headers=header)
with open("index_page.html", "wb") as f:
f.write(response.text.encode("utf-8"))
print ("ok") def zhihu_login(account, password):
#知乎登录
if re.match("^1\d{10}",account):
print ("手机号码登录")
post_url = "https://www.zhihu.com/login/phone_num"
post_data = {
"_xsrf": get_xsrf(),
"phone_num": account,
"password": password
}
else:
if "@" in account:
#判断用户名是否为邮箱
print("邮箱方式登录")
post_url = "https://www.zhihu.com/login/email"
post_data = {
"_xsrf": get_xsrf(),
"email": account,
"password": password
} response_text = session.post(post_url, data=post_data, headers=header)
session.cookies.save() zhihu_login("", "admin123")
# get_index()
print(is_login())

zhihu_requests_login

5.在shell调试中添加UserAgent

 scrapy shell -s USER_AGENT='...' url

6.JsonView插件

可以很好的可视化看json

7.写入html文件

with open(''e:/zhihu.html'',"wb") as f:
f.write(response.text.encode('utf-8'))

8.yield理解

  如果是yield item 会到pipelins中处理

  如果是yield Request 会到下载器去下载

9.在mysql中怎么去重,设置主键去重,主键冲突

解决:在插入的sql语句后面加上 ON DUPLICATE KEY UPDATE content=VALUES(content) #这是需要更新的内容

10.手动输入验证码(zhihu.login_requests.py)

 def get_captcha():
import time
t=str(int(time.time()*1000))
captcha_url="https://www.zhihu.com/captcha.gif?r={0}&type=login".format(t)
t=session.get(captcha_url,headers=header)
with open("captcha.jpg","wb") as f:
f.write(t.content)
f.close()
captcha=input("输入验证码:")
return captcha
#为什么是第五行是session,而不是requests?
#因为requests会重新建立一次绘画 session,这与后面的参数不符,输入的验证码并不是当前的验证码。

作者:今孝

出处:http://www.cnblogs.com/jinxiao-pu/p/6749332.html

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。

第5章 scrapy爬取知名问答网站的更多相关文章

  1. 第4章 scrapy爬取知名技术文章网站(2)

    4-8~9 编写spider爬取jobbole的所有文章 # -*- coding: utf-8 -*- import re import scrapy import datetime from sc ...

  2. 第4章 scrapy爬取知名技术文章网站(1)

    4-1 scrapy安装以及目录结构介绍 安装scrapy可以看我另外一篇博文:Scrapy的安装--------Windows.linux.mac等操作平台,现在是在虚拟环境中安装可能有不同. 1. ...

  3. scrapy爬取西刺网站ip

    # scrapy爬取西刺网站ip # -*- coding: utf-8 -*- import scrapy from xici.items import XiciItem class Xicispi ...

  4. Python——Scrapy爬取链家网站所有房源信息

    用scrapy爬取链家全国以上房源分类的信息: 路径: items.py # -*- coding: utf-8 -*- # Define here the models for your scrap ...

  5. Python3.6+Scrapy爬取知名技术文章网站

    爬取分析 伯乐在线已经提供了所有文章的接口,还有下一页的接口,所有我们可以直接爬取一页,再翻页爬. 环境搭建 Windows下安装Python: http://www.cnblogs.com/0bug ...

  6. Python网络爬虫 | Scrapy爬取妹子图网站全站照片

    根据现有的知识,写了一个下载妹子图(meizitu.com)Scrapy脚本,把全站两万多张照片下载到了本地. 网站的分析 网页的网址分析 打开网站,发现网页的网址都是以 http://www.mei ...

  7. Scrapy爬取某装修网站部分装修效果图

    爬取图片资源 spider文件 from scrapy.linkextractors import LinkExtractor from scrapy.spiders import CrawlSpid ...

  8. 44.scrapy爬取链家网站二手房信息-2

    全面采集二手房数据: 网站二手房总数据量为27650条,但有的参数字段会出现一些问题,因为只给返回100页数据,具体查看就需要去细分请求url参数去请求网站数据.我这里大概的获取了一下筛选条件参数,一 ...

  9. 43.scrapy爬取链家网站二手房信息-1

    首先分析:目的:采集链家网站二手房数据1.先分析一下二手房主界面信息,显示情况如下: url = https://gz.lianjia.com/ershoufang/pg1/显示总数据量为27589套 ...

随机推荐

  1. WM_COPYDATA+BHO+Qt实现进程间通信

    最近项目有一个需求:点击网页上某个按钮,通知Qt客户端.网页相关操作使用了BHO,BHO与Qt通信通过WB_COPYDATA,为什么这么麻烦呢,因为项目正好用到了BHO,可能还有其他方式,能直接通过网 ...

  2. c#进阶之lambda表达式

    阅读之前,先确保对委托有基本的了解,传送门 c#进阶之浅析委托和事件. lambda表达式雏形第一步 在委托那篇文章,绑定的的方法都是具名函数,为了简化书写,可以换成匿名函数 public deleg ...

  3. Kafka与.net core(一)安装

    1.安装JDK 目前官网不能直接下载,在网上找到1.8.0版本安装包下载到本地. 1.1.下载jdk并解压 [root@iz2zei2y693gtrgwlibzlwz java]# ls jdk1.. ...

  4. NOIP2013PUZZLE

    #include<cstdio> #include<cstring> #define MIN(A,B) (A)<(B)?(A):(B) using namespace s ...

  5. NOIP2015BLOCKADE c++ 代码

    #include<algorithm> #include<fstream> #include<functional> #include<iostream> ...

  6. B - Bridging signals (LIS)

    点击打开链接 B - Bridging signals 'Oh no, they've done it again', cries the chief designer at the Waferlan ...

  7. 2018年Android面试题含答案--适合中高级(上)

    这些面试题是我在今年年初换工作的时候整理,没有重点.包括java基础,数据结构,网络,Android相关等等.适合中高级工程师.由于内容过多,将会分为上下两部分.下部分跳转链接:http://www. ...

  8. CentOS7基础建站指南(笔记)

    由于前段时间腾讯云打折,所以买了一台小服务器,想着以后写几个小网站,博客什么的,但是一开始就遇到了难题,大概就是Linux服务器的配置问题,比如如何假设服务器,配置非root用户,配置服务器数据的非r ...

  9. XMPPManager 解析

    一.用户登录流程 用户登录流程.png 注意:XMPP核心文件,基于TCP的XML流的传输,XMPPFrame框架是通过代理的方式实现消息传递的 实现用户登录的步骤如下: 1.实例化XMPPStrea ...

  10. Cause: org.jetbrains.plugins.gradle.tooling.util.ModuleComponentIdentifierIm Lorg/gradle/api/artifacts/ModuleIdentifier;

    今天碰到一个问题, 正常的下载gradle到本地,然后到spring官网上通过他们提供的start.spring.io创建一个demo项目, 然后在idea中打开,并配置下载的gradle到idea中 ...