Selenium Webdriver / Beautifulsoup + 网页抓取 + 错误 416
Selenium Webdriver / Beautifulsoup + Web Scraping + Error 416
我在 Python 和 Proxy 中使用 selenium webdriver 进行网络抓取。
我想使用此抓取浏览超过 10k 页的单个站点。
问题 正在使用此代理我只能发送一次请求。当我在同一个 link 或本网站的另一个 link 上发送另一个请求时,我收到 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()
有什么解决办法吗??
查看以下链接中的 416 错误问题,似乎是某些缓存信息(可能是 cookie)造成了问题。您可以第一次发送请求,但后续发送请求失败。
https://webmasters.stackexchange.com/questions/17300/what-are-the-causes-of-a-416-error
416 Requested Range Not Satisfiable
尝试通过设置首选项或在每次发送请求后删除 cookie 来选择不保存 cookie。
profile.set_preference("network.cookie.cookieBehavior", 2);
Selenium 对我没有帮助,所以我通过在服务器阻塞请求代理时使用 beautifulsoup, the website has used security to block proxy whenever received request, so I am continuously changing proxyurl and User-Agent 解决了这个问题。
我在此处粘贴我的代码
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
注:
更改代理和用户代理并仅使用最新更新的代理
很少有服务器只接受特定国家的代理,在我的例子中,我使用了来自美国的代理
这个过程可能会很慢,你仍然可以废弃数据
我在 Python 和 Proxy 中使用 selenium webdriver 进行网络抓取。
我想使用此抓取浏览超过 10k 页的单个站点。
问题 正在使用此代理我只能发送一次请求。当我在同一个 link 或本网站的另一个 link 上发送另一个请求时,我收到 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()
有什么解决办法吗??
查看以下链接中的 416 错误问题,似乎是某些缓存信息(可能是 cookie)造成了问题。您可以第一次发送请求,但后续发送请求失败。
https://webmasters.stackexchange.com/questions/17300/what-are-the-causes-of-a-416-error 416 Requested Range Not Satisfiable
尝试通过设置首选项或在每次发送请求后删除 cookie 来选择不保存 cookie。
profile.set_preference("network.cookie.cookieBehavior", 2);
Selenium 对我没有帮助,所以我通过在服务器阻塞请求代理时使用 beautifulsoup, the website has used security to block proxy whenever received request, so I am continuously changing proxyurl and User-Agent 解决了这个问题。
我在此处粘贴我的代码
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
注:
更改代理和用户代理并仅使用最新更新的代理
很少有服务器只接受特定国家的代理,在我的例子中,我使用了来自美国的代理
这个过程可能会很慢,你仍然可以废弃数据