我试图通过网页抓取来获取所有项目名称和创建者姓名,并且大部分都在工作,但是当我尝试使用“加载更多”按钮抓取无限滚动页面时,我收到了“超时异常:消息:”。请让我知道什么是错的,我需要纠正什么。谢谢
以下是当前使用的代码:
from bs4 import BeautifulSoup
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome('/usr/local/bin/chromedriver')
driver.get("https://www.kickstarter.com/discover/advanced?sort=newest&seed=2695789&page=1/")
button = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR,'bttn keyboard-focusable bttn-medium bttn-primary theme--create fill-bttn-icon hover-fill-bttn-icon')))
button.click()
names=[]
creators=[]
soup = BeautifulSoup(driver.page_source)
for a in soup.findAll('div',{'class':'js-react-proj-card grid-col-12 grid-col-6-sm grid-col-4-lg'}):
name=a.find('div', attrs={'class':'clamp-5 navy-500 mb3 hover-target'})
creator=a.find('div', attrs={'class':'type-13 flex'})
names.append(name.h3.text)
creators.append(creator.text)
df = pd.DataFrame({'Name':names,'Creator':creators})
答案 0 :(得分:1)
您确实不需要使用 Beautiful Soup
和 selenium
。前往 requests
图书馆,轻松轻松获取。
import requests
import json
records = []
for i in range(5):
req = requests.get('https://www.kickstarter.com/discover/advanced?google_chrome_workaround&woe_id=0&sort=newest&seed=2695910&page='+str(i),
headers={'Accept': 'application/json',
'Content-Type': 'application/json'})
if(req.status_code == 200):
josn2 = req.json()
projects = josn2.get("projects")
for i in range(len(projects)):
print("Project Name - " + projects[i]['name'],end=' Created By - ')
print(projects[i]['creator'].get('name'))
print("----------------")
输出:
您可以向下滚动到页面,因为 loadmore 按钮加载内容的次数在 for 循环中进行了多次计数,您将获得所有内容。