Web抓取而不被阻止

问题描述 投票:0回答:1

我阅读了很多有关该主题的文章,也尝试了本文的一些建议,但仍然被屏蔽。

https://www.scraperapi.com/blog/5-tips-for-web-scraping

  1. IP轮换:完成我正在使用VPN并经常更改IP(显然不是在脚本执行期间)]]

  2. 设置一个真实的用户代理:没有运气的假用户代理

  3. 设置其他请求标头:曾与SeleniumWire一起尝试过,但如何与2同时使用??

  4. 在您的请求之间设置随机间隔:已完成,但是无论如何,目前我什至无法访问起始主页!!

  5. 设置引荐来源:与3相同。

  6. 使用无头浏览器:不知道

  7. 避免蜜罐陷阱:与4相同。

    1. 10:不相关

我要剪贴的网站:https://www.winamax.fr/paris-sportifs/

不使用硒:可以顺利进入包含一些游戏及其赔率的页面,我可以从这里导航

使用硒:页面显示“ Winamax est actuellement en maintenance”消息,没有游戏,没有赔率

尝试执行这段代码,您可能很快就会被阻塞:

from selenium import webdriver
import time
from time import sleep
import json

driver = webdriver.Chrome(executable_path="chromedriver")
driver.get("https://www.winamax.fr/paris-sportifs/")   #I'm even blocked here now !!!

toto = driver.page_source.splitlines()
titi = {}
matchez = []
matchez_detail = []
resultat_1 = {}
resultat_2 = {}
taratata = 1
comptine = 1

for tut in toto:
    if tut[0:53] == "<script type=\"text/javascript\">var PRELOADED_STATE = ": titi = json.loads(tut[53:tut.find(";var BETTING_CONFIGURATION = ")])

for p_id in titi.items():
    if p_id[0] == "sports": 
        for fufu in p_id:
            if isinstance(fufu, dict):
                for tyty in fufu.items():
                    resultat_1[tyty[0]] = tyty[1]["categories"]

for p_id in titi.items():
    if p_id[0] == "categories": 
        for fufu in p_id:
            if isinstance(fufu, dict):
                for tyty in fufu.items():
                    resultat_2[tyty[0]] = tyty[1]["tournaments"]

for p_id in resultat_1.items():
    for tgtg in p_id[1]:
        for p_id2 in resultat_2.items():
            if str(tgtg) == p_id2[0]: 
                for p_id3 in p_id2[1]:
                    matchez.append("https://www.winamax.fr/paris-sportifs/sports/"+str(p_id[0])+"/"+str(tgtg)+"/"+str(p_id3))

for alisson in matchez:
    print("compet " + str(taratata) + "/" + str(len(matchez)) + " : " + alisson)
    taratata = taratata + 1
    driver.get(alisson)
    sleep(1)
    elements = driver.find_elements_by_xpath("//*[@id='app-inner']/div/div[1]/span/div/div[2]/div/section/div/div/div[1]/div/div/div/div/a")
    for elm in elements:
        matchez_detail.append(elm.get_attribute("href"))

for mat in matchez_detail:
    print("match " + str(comptine) + "/" + str(len(matchez_detail)) + " : " + mat)
    comptine = comptine + 1
    driver.get(mat)
    sleep(1)
    elements = driver.find_elements_by_xpath("//*[@id='app-inner']//button/div/span")
    for elm in elements:
        elm.click()
        sleep(1) # and after my specific code to scrap what I want

我阅读了很多关于该主题的文章,也尝试了本文的一些建议,但是我仍然被阻止。 https://www.scraperapi.com/blog/5-tips-for-web-scraping IP轮播:确实我正在使用VPN ...

python selenium selenium-chromedriver
1个回答
1
投票

我建议使用请求,因为您说请求有效,所以我看不出使用硒的原因,只要您使用适当的标头,请求就可以在几乎所有站点上工作,您可以通过查看以下内容来查看所需的标头chrome或Firefox中的开发者控制台,然后查看请求标头。

© www.soinside.com 2019 - 2024. All rights reserved.