当前位置: 首页 > 知识库问答 >
问题:

如何向下滚动并单击按钮连续web刮页在python

孟跃
2023-03-14

我想刮掉整个页面,以获得帐户的链接,但问题是:

>

  • 我需要多次单击“加载更多”按钮,以获取要刮取的完整帐户列表

    有一个偶尔出现的弹出窗口,所以我如何检测它并单击取消按钮

    如果可能的话,那么我更喜欢只请求刮掉整个页面。因为我必须点击按钮,所以想到使用硒。

    这是我的代码:

    import time
    import requests
    from bs4 import BeautifulSoup
    import lxml
    from selenium import webdriver
    
    driver = webdriver.Chrome()
    driver.get('https://society6.com/franciscomffonseca/followers')
    
    time.sleep(3)
    
    try: driver.find_element_by_class_name('bx-button').click() #button to remove popup
    
    except: print("no popups")
    
    driver.find_element_by_class_name('loadMore').click #to click load more button
    

    我正在使用一个测试页面,其中有10K追随者,并希望刮掉他们的追随者帐户链接。我已经代码刮刀所以只需要看到完整的网页

    https://society6.com/franciscomffonseca/followers

    刮码以防万一:

    r2 = requests.get('https://society6.com/franciscomffonseca/followers')
    print(r2.status_code)
    r2.raise_for_status
    
    soup2 = BeautifulSoup(r2.content, "html.parser")
    a2_tags = soup2.find_all(attrs={"class": "user"})
    
    #attrs={"class": "user-list clearfix"}
    
    follow_accounts = []
    
    for a2 in a2_tags:
        follow_accounts.append('https://society6.com'+a2['href'])
    
    print(follow_accounts)
    print("number of accounts scraped: " + str(len(follow_accounts)))
    

    加载更多按钮:

    <button class="loadMore" onclick="loadMoreFollowers();">Load More</button>
  • 共有1个答案

    裴卓君
    2023-03-14

    您可以直接向Society6 API发出以下请求:

    counter = 1
    
    while True:
        source = requests.get('https://society6.com/api/users/franciscomffonseca/followers?page=%s' % counter).json()
        if source['data']['attributes']['followers']:
            for i in source['data']['attributes']['followers']:
                print(i['card']['link']['href'])
            counter += 1
        else:
            break
    

    这会将相对的HREF打印为

    /wickedhonna
    /wiildrose
    /williamconnolly
    /whiteca1x
    

    如果你想要绝对的hrefs,只需替换

    print(i['card']['link']['href'])
    

    具有

    print("https://society6.com" + i['card']['link']['href'])
    
     类似资料:
    • 我正试图让JQuery在按下下一个按钮时自动单击该按钮。在互联网上,我发现应该是这样的(查看JQuery部分)。但由于某种原因,它不起作用。 它们必须在同一个代码中吗? 我尝试过的:JQuery: 表单中的按钮1 HTML: 表单中的按钮2 HTML:

    • 问题内容: 我对Linkedin中技能部分的“加载更多”按钮感到困惑。我 在找到按钮的xpath时收到此错误: 问题是我的元素在页面上不可见,因此我一直试图 找到一种方法来在页面上连续滚动直到按钮可见。 我正在尝试对多个配置文件进行forloop。 我的相关代码: Attempts 1. 随着 页面滚动就在元件的下方,因此该元素不再是可见的,随后发生同样的错误。 我也尝试过move_to_elem

    • 链接到我正在尝试刮取的页面: https://www.nytimes.com/reviews/dining 因为这个页面有一个“show more”按钮,所以我需要Selenium自动反复单击“show more”按钮,然后以某种方式使用Beauty soup来获取页面上每个餐厅评论的链接。在下面的照片中,我想获取的链接位于https://...onigiri.html" 迄今为止的代码: 我如何

    • 问题内容: 昨天我发布了一个问题,关于必须按下两次按钮才能使其正常工作。我得到了很好的帮助,这是stackoverflow的标志,但是问题仍然存在。我将代码缩减到最低限度,问题仍然存在。我仔细阅读了BalusC的建议,希望能在表单内找到表单。我当然看不到任何东西,所以我将发布我的代码,以希望更多的眼睛看到一些东西。 我有一个模板,可以从“欢迎”(登录部分)中调用。这将转到具有命令按钮的userIn

    • 不含硒 我需要点击一个网页上的按钮。有可能与请求有关吗?我不想用硒。 提前道谢!

    • 编辑:底部的解决方案 这是一个跳棋游戏。单击一个按钮后,它等待单击第二个按钮与之交换。然而,有时你可能不想移动那个按钮,但一旦你点击了它,就没有回头路了,因为我无法禁用它。 在这里的其他帖子中,我看到人们使用 这只是使它在第一次单击后不可见。 这什么都干不了。 这也没什么用。编辑:所有这些方法都用true和false进行了尝试。 私有无效交换(){ 但你也需要 这样它就重新启用了它,或者其他什么,