【发布时间】:2019-09-03 15:05:51
【问题描述】:
编辑:可能的重复并不能解决我的问题,因为我也尝试使用无头浏览器但没有成功。该问题并未解释如何使用无头浏览器来完成此任务或类似任务。
我正在抓取这个页面:
前 12 个产品是自动加载的(不使用 JS),然后其他(我相信是 48 个?)产品在用户向下滚动一点后加载。
这个sn-p:
import requests
import csv
import io
import os
import re
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from datetime import datetime
from bs4 import BeautifulSoup
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'
}
url_list2 = []
data2 = requests.get("https://www.finishline.com/store/men/shoes/_/N-1737dkj?mnid=men_shoes#/store/men/shoes/nike/adidas/jordan/under-armour/puma/new-balance/reebok/champion/timberland/fila/lacoste/converse/_/N-1737dkjZhtjl46Zh51uarZvnhst2Zu4e113Z16ggje2Z1alnhbgZ1lzobj2Z7oi4waZ1hzyzukZm0ym0nZj4k440Zdshbsy?mnid=men_shoes",headers=headers)
soup2 = BeautifulSoup(data2.text, 'html.parser')
x = soup2.findAll('div', attrs={'class': 'product-card'})
for url2 in x:
get_urls = "https://www.finishline.com"+url2.find('a')['href']
url_list2.append(get_urls)
print(url_list2)
会得到12个独立于JS的产品(这可以通过在Chrome设置中关闭JS来检查)。但是打开 JS 后,页面上有 60 个(或 59 个)产品。
如何使用 BS4 获得所有产品?我也尝试过 Selenium,但是使用它会出现不同的错误。
在 Selenium 尝试中,我设法获得了页面上显示的所有 59 种产品。我正在使用此代码获取所有产品页面的 URL,以便进一步抓取。
import requests
import csv
import io
import os
import time
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver import DesiredCapabilities
from bs4 import BeautifulSoup,Tag
page = "https://www.finishline.com/store/men/shoes/_/N-1737dkj?mnid=men_shoes#/store/men/shoes/nike/adidas/jordan/under-armour/puma/new-balance/reebok/champion/timberland/fila/lacoste/converse/_/N-1737dkjZhtjl46Zh51uarZvnhst2Zu4e113Z16ggje2Z1alnhbgZ1lzobj2Z7oi4waZ1hzyzukZm0ym0nZj4k440Zdshbsy?mnid=men_shoes"
url_list2 = []
page_num = 0
#session = requests.Session()
while page_num <1160:
driver = webdriver.Chrome()
driver.get(page)
getproductUrls = driver.find_elements_by_class_name('product-card')
for url2 in getproductUrls:
get_urls = "https://www.finishline.com"+url2.find_element_by_tag_name('a').get_attribute("href")
url_list2.append(get_urls)
print(url_list2)
driver.close()
page = "https://www.finishline.com/store/men/shoes/_/N-1737dkj?mnid=men_shoes#/store/men/shoes/nike/adidas/jordan/under-armour/puma/new-balance/reebok/champion/timberland/fila/lacoste/converse/_/N-1737dkjZhtjl46Zh51uarZvnhst2Zu4e113Z16ggje2Z1alnhbgZ1lzobj2Z7oi4waZ1hzyzukZm0ym0nZj4k440Zdshbsy?mnid=men_shoes&No={}".format(page_num)
page_num +=40
但是,过了一会儿,错误
raise exception_class(message, screen, stacktrace, alert_text)
selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: None
Message: unexpected alert open: {Alert text : something went wrong}
发生,因为站点检测到异常活动。如果我在浏览器中打开网站 finishline.com,我会收到“拒绝访问”消息,并且必须清除我的 cookie 并刷新它才能再次工作。显然,在弹出此消息之前,我的脚本还没有完成。
有人知道解决方案吗?提前谢谢你。
【问题讨论】:
标签: python selenium web-scraping beautifulsoup