【发布时间】:2019-03-30 15:24:46
【问题描述】:
我正在使用 selenium 和 phantomJS 获取页面,并使用 BeautifulSoup 来报废数据。此代码有时有效,但大多数时候无效。我使用的 URL 是 Google Flight。 我无法理解是什么导致它如此失败。驱动程序返回 html 内容但没有屏幕截图。这是代码:
def update_ds():
print("Called")
url = "https://www.google.fr/flights#flt=DEL.r/m/02j9z.2018-11-10*r/m/02j9z.DEL.2018-11-14;c:USD;e:1;ls:1w;sd:0;t:e"
driver = webdriver.PhantomJS(executable_path='C:\\phantomjs-2.1.1-windows\\bin\\phantomjs')
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36")
driver = webdriver.PhantomJS(executable_path='C:\\phantomjs-2.1.1-windows\\bin\\phantomjs',desired_capabilities=dcap,service_args = ['--ignore-ssl-errors=true'])
driver.implicitly_wait(120)
driver.get(url)
driver.save_screenshot('flight.png')
html_content = driver.page_source
#print(html_content)
print("Connected")
s = BeautifulSoup(html_content,"lxml")
best_price_tags = s.find_all('span',class_=['uKOpFp4SF2X__price flt-subhead2','uKOpFp4SF2X__price flt-subhead2 uKOpFp4SF2X__deal'])
print("tags ",len(best_price_tags))
best_price = []
for tag in best_price_tags:
best_price.append(int(tag.string.replace('US$','').replace(',','')))
【问题讨论】:
-
看起来您并没有等待 webapp 加载内容 -
implicitly_wait在get(url)之前。您需要再次等待,因为 driver.get() 不会等待所有ajax请求完成。
标签: python selenium-webdriver web-scraping beautifulsoup