【发布时间】:2015-09-05 16:00:17
【问题描述】:
我正在尝试从航班搜索页面中抓取一些数据。
这个页面是这样工作的:
您填写表格,然后单击按钮搜索 - 没关系。当您单击按钮时,您将被重定向到带有结果的页面,这就是问题所在。此页面连续添加结果,例如一分钟,这没什么大不了的 - 问题是要获得所有这些结果。当你在真正的浏览器中时,你必须向下滚动页面,这些结果就会出现。所以我尝试使用 Selenium 向下滚动。它在页面底部向下滚动可能非常快,或者它是跳转而不是滚动页面不会加载任何新结果。
当您缓慢向下滚动时,它会重新加载结果,但如果您快速滚动,它会停止加载。
我不确定我的代码是否有助于理解这一点,所以我附上了它。
SEARCH_STRING = """URL"""
class spider():
def __init__(self):
self.driver = webdriver.Firefox()
@staticmethod
def prepare_get(dep_airport,arr_airport,dep_date,arr_date):
string = SEARCH_STRING%(dep_airport,arr_airport,arr_airport,dep_airport,dep_date,arr_date)
return string
def find_flights_html(self,dep_airport, arr_airport, dep_date, arr_date):
if isinstance(dep_airport, list):
airports_string = str(r'%20').join(dep_airport)
dep_airport = airports_string
wait = WebDriverWait(self.driver, 60) # wait for results
self.driver.get(spider.prepare_get(dep_airport, arr_airport, dep_date, arr_date))
wait.until(EC.invisibility_of_element_located((By.XPATH, '//img[contains(@src, "loading")]')))
wait.until(EC.invisibility_of_element_located((By.XPATH, u'//div[. = "Poprosíme o trpezlivosť, hľadáme pre Vás ešte viac letov"]/preceding-sibling::img')))
self.driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")
self.driver.find_element_by_xpath('//body').send_keys(Keys.CONTROL+Keys.END)
return self.driver.page_source
@staticmethod
def get_info_from_borderbox(div):
arrival = div.find('div',class_='departure').text
price = div.find('div',class_='pricebox').find('div',class_=re.compile('price'))
departure = div.find_all('div',class_='departure')[1].contents
date_departure = departure[1].text
airport_departure = departure[5].text
arrival = div.find_all('div', class_= 'arrival')[0].contents
date_arrival = arrival[1].text
airport_arrival = arrival[3].text[1:]
print 'DEPARTURE: '
print date_departure,airport_departure
print 'ARRIVAL: '
print date_arrival,airport_arrival
@staticmethod
def get_flights_from_result_page(html):
def match_tag(tag, classes):
return (tag.name == 'div'
and 'class' in tag.attrs
and all([c in tag['class'] for c in classes]))
soup = mLib.getSoup_html(html)
divs = soup.find_all(lambda t: match_tag(t, ['borderbox', 'flightbox', 'p2']))
for div in divs:
spider.get_info_from_borderbox(div)
print len(divs)
spider_inst = spider()
print spider.get_flights_from_result_page(spider_inst.find_flights_html(['BTS','BRU','PAR'], 'MAD', '2015-07-15', '2015-08-15'))
所以在我看来,主要问题是它滚动得太快而无法触发新的结果加载。
你知道如何让它工作吗?
【问题讨论】:
-
软滚动和滚动在此分享:stackoverflow.com/a/69324762/7118138
标签: python selenium scroll selenium-webdriver