欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

講解Python的Scrapy爬蟲框架使用代理進行采集的方法

 更新時間:2016年02月18日 15:13:51   投稿:goldensun  
這篇文章主要介紹了講解Python的Scrapy爬蟲框架使用代理進行采集的方法,并介紹了隨機使用預先設好的user-agent來進行爬取的用法,需要的朋友可以參考下

1.在Scrapy工程下新建“middlewares.py”

# Importing base64 library because we'll need it ONLY in case if the proxy we are going to use requires authentication
import base64

# Start your middleware class
class ProxyMiddleware(object):
 # overwrite process request
 def process_request(self, request, spider):
  # Set the location of the proxy
  request.meta['proxy'] = "http://YOUR_PROXY_IP:PORT"

  # Use the following lines if your proxy requires authentication
  proxy_user_pass = "USERNAME:PASSWORD"
  # setup basic authentication for the proxy
  encoded_user_pass = base64.encodestring(proxy_user_pass)
  request.headers['Proxy-Authorization'] = 'Basic ' + encoded_user_pass

2.在項目配置文件里(./project_name/settings.py)添加

DOWNLOADER_MIDDLEWARES = {
 'scrapy.contrib.downloadermiddleware.httpproxy.HttpProxyMiddleware': 110,
 'project_name.middlewares.ProxyMiddleware': 100,
}

只要兩步,現(xiàn)在請求就是通過代理的了。測試一下^_^

from scrapy.spider import BaseSpider
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.http import Request

class TestSpider(CrawlSpider):
 name = "test"
 domain_name = "whatismyip.com"
 # The following url is subject to change, you can get the last updated one from here :
 # http://www.whatismyip.com/faq/automation.asp
 start_urls = ["http://xujian.info"]

 def parse(self, response):
  open('test.html', 'wb').write(response.body)

3.使用隨機user-agent

默認情況下scrapy采集時只能使用一種user-agent,這樣容易被網(wǎng)站屏蔽,下面的代碼可以從預先定義的user- agent的列表中隨機選擇一個來采集不同的頁面

在settings.py中添加以下代碼

DOWNLOADER_MIDDLEWARES = {
  'scrapy.contrib.downloadermiddleware.useragent.UserAgentMiddleware' : None,
  'Crawler.comm.rotate_useragent.RotateUserAgentMiddleware' :400
 }

注意: Crawler; 是你項目的名字 ,通過它是一個目錄的名稱 下面是蜘蛛的代碼

#!/usr/bin/python
#-*-coding:utf-8-*-

import random
from scrapy.contrib.downloadermiddleware.useragent import UserAgentMiddleware

class RotateUserAgentMiddleware(UserAgentMiddleware):
 def __init__(self, user_agent=''):
  self.user_agent = user_agent

 def process_request(self, request, spider):
  #這句話用于隨機選擇user-agent
  ua = random.choice(self.user_agent_list)
  if ua:
   request.headers.setdefault('User-Agent', ua)

 #the default user_agent_list composes chrome,I E,firefox,Mozilla,opera,netscape
 #for more user agent strings,you can find it in http://www.useragentstring.com/pages/useragentstring.php
 user_agent_list = [\
  "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1"\
  "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",\
  "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6",\
  "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6",\
  "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1",\
  "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5",\
  "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5",\
  "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",\
  "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",\
  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",\
  "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",\
  "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",\
  "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",\
  "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",\
  "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",\
  "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.0 Safari/536.3",\
  "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24",\
  "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24"
  ]

相關文章

  • Python MySQL數(shù)據(jù)庫基本操作及項目示例詳解

    Python MySQL數(shù)據(jù)庫基本操作及項目示例詳解

    這篇文章主要介紹了Python連接MySQL數(shù)據(jù)庫后的一些基本操作,并以銀行管理系統(tǒng)項目為例,為大家具體介紹了一下部分功能的實現(xiàn),文中的示例代碼具有一定的學習價值,感興趣的可以了解一下
    2021-12-12
  • Python+OpenCV實現(xiàn)黑白老照片上色功能

    Python+OpenCV實現(xiàn)黑白老照片上色功能

    我們都知道,有很多經(jīng)典的老照片,受限于那個時代的技術,只能以黑白的形式傳世。盡管黑白照片別有一番風味,但是彩色照片有時候能給人更強的代入感。本文就來用Python和OpenCV實現(xiàn)老照片上色功能,需要的可以參考一下
    2023-02-02
  • 對Python 3.2 迭代器的next函數(shù)實例講解

    對Python 3.2 迭代器的next函數(shù)實例講解

    今天小編就為大家分享一篇對Python 3.2 迭代器的next函數(shù)實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • Python使用Flask-SQLAlchemy連接數(shù)據(jù)庫操作示例

    Python使用Flask-SQLAlchemy連接數(shù)據(jù)庫操作示例

    這篇文章主要介紹了Python使用Flask-SQLAlchemy連接數(shù)據(jù)庫操作,簡單介紹了flask、Mysql-Python以及Flask-SQLAlchemy的安裝方法,并結合實例形式分析了基于Flask-SQLAlchemy的數(shù)據(jù)庫連接相關操作技巧,需要的朋友可以參考下
    2018-08-08
  • MacOS(M1芯片?arm架構)下安裝tensorflow的詳細過程

    MacOS(M1芯片?arm架構)下安裝tensorflow的詳細過程

    這篇文章主要介紹了MacOS(M1芯片?arm架構)下如何安裝tensorflow,本節(jié)使用的版本是tensorflow2.4?python3.8,因此并未安裝加速插件,本文結合實例代碼詳細講解,需要的朋友可以參考下
    2023-02-02
  • Python3多線程爬蟲實例講解代碼

    Python3多線程爬蟲實例講解代碼

    本篇文章主要介紹了Python3多線程爬蟲實例講解代碼,Python3使用threading模塊來進行多線程,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • Python實現(xiàn)飛機大戰(zhàn)項目

    Python實現(xiàn)飛機大戰(zhàn)項目

    這篇文章主要為大家詳細介紹了Python實現(xiàn)飛機大戰(zhàn)項目,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • 基于Python實現(xiàn)成語填空游戲的示例代碼

    基于Python實現(xiàn)成語填空游戲的示例代碼

    成語填空想必大家都是十分熟悉的了,特別是有在上小學的家長肯定都有十分深刻的印象。當然了你也別小看了成語調(diào)控小游戲,有的時候知識儲備不夠,你還真的不一定猜得出來是什么。本文就來用Python編寫一個簡單的成語填空游戲,感興趣的可以了解下
    2023-02-02
  • 用python爬取中國大學排名網(wǎng)站排名信息

    用python爬取中國大學排名網(wǎng)站排名信息

    大家好,本篇文章主要講的是用python爬取中國大學排名網(wǎng)站排名信息,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • Python 數(shù)值區(qū)間處理_對interval 庫的快速入門詳解

    Python 數(shù)值區(qū)間處理_對interval 庫的快速入門詳解

    今天小編就為大家分享一篇Python 數(shù)值區(qū)間處理_對interval 庫的快速入門詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11

最新評論