Selenium:向下滚动页面并使用python进行解析
我尝试解析页面 我有一些问题。 我应该滚动页面,然后获取所有Selenium:向下滚动页面并使用python进行解析,python,html,selenium,beautifulsoup,Python,Html,Selenium,Beautifulsoup,我尝试解析页面 我有一些问题。 我应该滚动页面,然后获取所有htmlcode。 但我滚动页面,高度在变化,但解析的结果是错误的,因为它只返回第一页的结果。 我不明白,我应该更新网页的html代码,我该怎么做 def get_link_product_ozon(url): chromedriver = "chromedriver" os.environ["webdriver.chrome.driver"] = chromedriver driver = webdriver.
html
code。
但我滚动页面,高度在变化,但解析的结果是错误的,因为它只返回第一页的结果。
我不明白,我应该更新网页的html代码,我该怎么做
def get_link_product_ozon(url):
chromedriver = "chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
driver.get(url)
i = 0
last_height = driver.execute_script("return document.body.scrollHeight")
while i < 80:
try:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3)
new_height = driver.execute_script("return document.body.scrollHeight")
i += 1
last_height = new_height
except:
time.sleep(3)
continue
soup = BeautifulSoup(driver.page_source, "lxml")
all_links = soup.findAll('div', class_='bOneTile inline jsUpdateLink mRuble ')
for link in all_links:
print(link.attrs['data-href'])
driver.close()
def get_link_product_ozon(url):
chromedriver=“chromedriver”
os.environ[“webdriver.chrome.driver”]=chromedriver
driver=webdriver.Chrome(chromedriver)
获取驱动程序(url)
i=0
last\u height=driver.execute\u脚本(“return document.body.scrollHeight”)
当我<80时:
尝试:
执行脚本(“window.scrollTo(0,document.body.scrollHeight);”)
时间。睡眠(3)
new\u height=driver.execute\u脚本(“returndocument.body.scrollHeight”)
i+=1
最后高度=新高度
除:
时间。睡眠(3)
持续
soup=BeautifulSoup(driver.page_来源,“lxml”)
所有链接=soup.findAll('div',class='bOneTile inline jsUpdateLink mRuble')
对于所有链接中的链接:
打印(link.attrs['data-href'])
驱动程序关闭()
滚动后加载的div没有classmRuble
,您正在进行精确的字符串匹配。也许可以尝试以下方式:
all_links = soup.select('div.bOneTile.inline.jsUpdateLink')
all_links = soup.select('div[data-href]')
...