Как прокрутить вниз и нажать кнопку для непрерывного удаления страниц в Python - PullRequest
0 голосов
/ 17 сентября 2018

Я хочу просмотреть всю страницу, чтобы получить ссылки на аккаунт, но проблема заключается в следующем:

  1. Мне нужно много раз нажимать кнопку Load more, чтобы получить полный список учетных записей для удаления

  2. Время от времени появляется всплывающее окно, поэтому как мне его обнаружить и нажать кнопку отмены

Если возможно, тогда я предпочитаю удалить всю страницутолько с запросом.Так как я должен нажимать кнопки, поэтому подумал об использовании селена.

Вот мой код:

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

Я использую тестовую страницу, на которой есть 10 000 подписчиков и я хочу удалить ссылку на учетную запись своих подписчиков.,Я уже закодировал скребок, так что просто нужно увидеть полную веб-страницу

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 scrapped: " + str(len(follow_accounts)))

HTML load more кнопка:

<button class="loadMore" onclick="loadMoreFollowers();">Load More</button>

1 Ответ

0 голосов
/ 17 сентября 2018

Вы можете сделать прямой запрос к API Society6, как показано ниже:

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

Это будет печатать относительные ссылки как

/wickedhonna
/wiildrose
/williamconnolly
/whiteca1x

Если вы хотите абсолютные hrefs, просто замените

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

с

print("https://society6.com" + i['card']['link']['href'])
...