我正在使用 selenium 在agoda 上搜索并抓取页面中的所有酒店名称,但输出仅返回 2 个名称。然后我尝试添加一行滚动到底部,现在输出给了我前 2 个名字和后 2 个名字(从头开始的前两个,从底部开始的最后两个)我不明白有什么问题,我为每一步都添加了 time.sleep() 所以整个页面应该已经完全加载了。selenium 是否通过页面视图限制它只能刮掉那些可见的元素?我的代码如下:driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")time.sleep(30)def scrape(): r = requests.get(current_page)if r.status_code == requests.codes.ok: print('start scraping!') hotel = driver.find_elements_by_class_name('hotel-name') hotels = [] for h in hotel: if hotel: hotels.append(h.text) print(hotels, file=open("output.txt", 'a', encoding="utf-8")) scrape()
添加回答
举报
0/150
提交
取消