我正在使用代理在Python中使用selenium进行web抓取。
我想浏览超过10k页的单一网站使用这个刮刮。
问题正在使用这个代理,我只能一次性发送请求。当我在同一个链接或这个站点的另一个链接上发送另一个请求时,我得到416个错误(有点像使用防火墙的IP块),时间为1-2小时。
注意:我可以用这个代码来抓取所有的普通站点,但是这个站点有某种安全性,这阻止了我的抓取。
这是密码。
profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference(
"network.proxy.http", "74.73.148.42")
profile.set_preference("network.proxy.http_port", 3128)
profile.update_preferences()
browser = webdriver.Firefox(firefox_profile=profile)
browser.get('http://www.example.com/')
time.sleep(5)
element = browser.find_elements_by_css_selector(
'.well-sm:not(.mbn) .row .col-md-4 ul .fs-small a')
for ele in element:
print ele.get_attribute('href')
browser.quit()有什么解决办法吗?
发布于 2015-10-21 12:26:35
Selenium对我没有帮助,所以我用美汤解决了这个问题,网站在收到请求时就使用安全性来阻止代理,所以每当服务器阻塞请求代理时,我就会不断地更改丙羟和用户代理。
我在这里粘贴我的代码
from bs4 import BeautifulSoup
import requests
import urllib2
url = 'http://terriblewebsite.com/'
proxy = urllib2.ProxyHandler({'http': '130.0.89.75:8080'})
# Create an URL opener utilizing proxy
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
request = urllib2.Request(url)
request.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15')
result = urllib2.urlopen(request)
data = result.read()
soup = BeautifulSoup(data, 'html.parser')
ptag = soup.find('p', {'class', 'text-primary'}).text
print ptag注:
这个过程可能很慢,但你还是可以把数据报废。
发布于 2015-10-16 22:10:57
通过以下链接中的416个错误问题,似乎有些缓存的信息(可能是cookie)正在创建这些问题。您可以第一次发送请求,而随后的发送请求将失败。
https://webmasters.stackexchange.com/questions/17300/what-are-the-causes-of-a-416-error 416要求的范围不可满足
尝试通过设置首选项或在每次发送请求后删除cookie来选择不保存cookie。
profile.set_preference("network.cookie.cookieBehavior", 2);https://stackoverflow.com/questions/32738640
复制相似问题