【问题标题】:Webscraping the html from a page but I need the cookies从页面抓取 html 但我需要 cookie
【发布时间】:2022-01-02 02:04:09
【问题描述】:

一段时间以来,我一直在尝试对这个网站进行网络抓取(我是业余爱好者):“https://www.coches.net/segunda-mano/”,但我一直无法这样做我对如何解决它有一些想法,但都没有奏效。

我注意到网站的这一部分没有防刮保护“https://www.coches.net/km-0/”所以我尝试从那里获取标头请求的 cookie,但它没有工作。

我阅读了我认为会生成 cookie 的 javascript,但我对 java 脚本一无所知,而且代码只是一团糟(“https://www.coches.net/ztkieflaaxcvaiwh2”)。

最后我做的是购买可以抓取 html 的 octoparse 的免费试用版,然后我使用 octoparse API 向 python 请求该数据,但我不能再使用这个了,因为在他们的服务器中存储数据/脚本需要你有一个高级订阅,我无法为我所做的小项目每个月支付,所以,我只是想知道是否有一种方法可以模拟 octoparse 在 python 中的作用或生成所需的 cookie我的请求标头要通过。

我还查看了 cookie,发现每次我因执行过多请求而被阻止时,我只需手动执行验证码,然后网站会重置并给我新的 cookie,这就是饼干:

_hjid= stays the same
borosTcf= stays the same
ajs_anonymous_id= stays the same
_pbjs_userid_consent_data= stays the same
_gcl_au= stays the same
__gads= stays the same
gig_bootstrap_3_ejKPtiTCoMZOmiD2PJgl0GYbIQOdeBma77joBheqTs15Nx5EkD9evJSOuefj2S6H= stays the same
euconsent-v2= stays the same
cfg= stays the same
_hjSession_48459= stays the same
_hjIncludedInSessionSample= stays the same
_hjAbsoluteSessionInProgress= stays the same
AMCVS_05FF6243578784B37F000101@AdobeOrg= stays the same
AMCV_05FF6243578784B37F000101@AdobeOrg= stays the same
reese84=3: This one is different each time
cto_bundle= This one is different each time

感谢任何帮助,这让我发疯了,我曾经抓取的其他网站只需要一个简单的标头结构和简单的数据负载,但我是新手,所以在某些时候我不得不寻求帮助。

【问题讨论】:

    标签: python web-scraping cookies python-requests


    【解决方案1】:

    Cookie 是一种方法。另一种方法是导入您的用户资料。在本例中,我假设您将使用 Chrome 以及 Selenium 库:

    from selenium import webdriver
    
    
    userdatadir = 'C:/Users/USER/AppData/Local/Google/Chrome/User Data' #path may be different for you
    chromeOptions.add_argument(f"--user-data-dir={userdatadir}")
    driver = webdriver.Chrome(options=chromeOptions)
    driver.get("https://www.coches.net/segunda-mano/")
    

    基本上它的作用是打开一个浏览器,就像您在搜索网络一样

    【讨论】:

    • 在使用 selenium 1 次后它会阻止我。
    猜你喜欢
    • 2012-03-26
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    • 2016-12-06
    相关资源
    最近更新 更多