我想剪贴整个页面以获得帐户链接,但问题是:
我需要多次点击Load more
按钮才能获取要删除的完整帐户列表
偶尔会出现一个弹出窗口,所以我该如何检测它并单击“取消”按钮
如果可能的话,我宁愿只用请求将整个页面抓取。由于我必须单击按钮,因此考虑使用硒。
这是我的代码:
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
我正在使用一个测试页面,该页面包含1万个关注者,并希望取消其关注者帐户链接。我已经为抓取程序编写了代码,因此只需要查看完整的网页
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)))
load more
按钮的HTML:
<button class="loadMore" onclick="loadMoreFollowers();">Load More</button>
答案 0 :(得分:4)
您可以直接向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
如果要使用绝对href,只需替换
print(i['card']['link']['href'])
使用
print("https://society6.com" + i['card']['link']['href'])