2012-09-17 29 views
4

这些都是在蟒蛇履带的定义:如何代理添加到BeautifulSoup履带

from __future__ import with_statement 

from eventlet.green import urllib2 
import eventlet 
import re 
import urlparse 
from bs4 import BeautifulSoup, SoupStrainer 
import sqlite3 
import datetime 

如何我添加了一个旋转的代理(每线程开一个代理),以递归cralwer上BeautifulSoup工作?

我知道,如果我是用机械化的浏览器中添加的代理:

br = Browser() 
br.set_proxies({'http':'http://username:[email protected]:port', 
'https':'https://username:[email protected]:port'}) 

,但我想知道具体是什么样的解决方案将BeautifulSoup需要。

非常感谢您的帮助!

回答

0

抬起头,有一个不太复杂的解决方案,这可现在,共享here看看BeautifulSoup的例子:

import requests 

proxies = {"http": "http://10.10.1.10:3128", 
      "https": "http://10.10.1.10:1080"} 

requests.get("http://example.org", proxies=proxies) 

然后做从请求响应中恢复正常。

所以,如果你想单独的线程与不同的代理,你可以为每个请求调用不同的字典条目(例如从一个字典列表)。

当您现有的包使用已经是请求/ bs4时,这似乎更直接实施,因为它只是在您现有的requests.get()调用中添加的额外**kwargs。您不必为每个线程初始化/安装/打开单独的urllib处理程序。