爬蟲框架學習

 

1. request.post 與request.data的區別 

 

 

viewspython

from django.shortcuts import render, HttpResponse

def index(request):

    print(request.body)
    #輸出結果:b'username=alex&password=123'

    print(request.POST)  #request.POST解析不了JSON格式.

    return HttpResponse('......')

request模塊模擬發送數據django

import requests


#請求體格式: # requests.data獲得的數據 :b'username=alex&password=123' # requests.POST獲得的數據 :<QueryDict: {'username': ['alex'], 'password': ['123']}>

#以urlencoded格式發送過去的.

r1 =requests.post(
    url ="http://127.0.0.1:8000/index/",
    data ={"username":"alex","password":123},
  )
print(r1.text)


#請求體格式: # requests.data獲得的數據 :b
'username=alex&password=123' # requests.POST獲得的數據 :<QueryDict: {}> #以json字符串格式發送 POST裏拿不到數據. r1 =requests.post( url ="http://127.0.0.1:8000/index/", json ={"username":"alex","password":123}, ) print(r1.text)

 

 

 2 .scrapy的安裝工做

 

a. 下載twisted 
http://www.lfd.uci.edu/~gohlke/pythonlibs/#twistedjson

 

b. 安裝wheel 
pip3 install wheelapp

 

c. 安裝twisted (進入下載到twisted的目錄裏進行安裝.)
pip3 install Twisted‑18.7.0‑cp36‑cp36m‑win_amd64.whlscrapy

 

d. 安裝pywin32
pip3 install pywin32ide

 

e. 安裝scrapy 
pip3 install scrapy post

 

f. 建立爬蟲項目url

scrapy startproject  xzxspa

建立spider3d

scrapy genspider chouti  chouti.com  (genspider至關於app)

 

 

 

1. scrapy 
        
        如何建立一個django程序?
            django-admin startproject mysite 
            
            cd mysite
            
            python manage.py startapp app01 
            
            python manage.py runserver 
        如何建立一個scrapy程序?
            scrapy startproject xzx 
            
            cd xzx 
            
            scrapy genspider chouti chouti.com 
            
            scrapy crawl chouti --nolog
相關文章
相關標籤/搜索