【发布时间】:2018-03-26 21:53:04
【问题描述】:
我正在尝试抓取以下网站“url='https://angel.co/life-sciences' "。该网站包含超过 8000 条数据。从这个页面我需要公司名称和链接、加入日期和关注者等信息。在此之前,我需要通过单击按钮对关注者列进行排序。然后通过单击更多隐藏加载更多信息按钮。该页面最多可点击 20 次(更多隐藏)内容,之后它不会加载更多信息。但我只能通过排序获取顶级关注者信息。这里我已经实现了 click() 事件,但它是显示错误。
Unable to locate element: {"method":"xpath","selector":"//div[@class="column followers sortable sortable"]"} #before edit this was my problem, using wrong class name
那么我需要在这里给更多的睡眠时间吗?(尝试给这个但同样的错误)
我需要解析所有上述信息,然后访问这些网站的单个链接以仅抓取该 html 页面的内容 div。
请给我建议一种方法
这是我目前的代码,我没有使用beautifulsoup添加html解析部分。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from time import sleep
from selenium import webdriver
from bs4 import BeautifulSoup
#import urlib2
driver = webdriver.Chrome()
url='https://angel.co/life-sciences'
driver.get(url)
sleep(10)
driver.find_element_by_xpath('//div[@class="column followers sortable"]').click()#edited
sleep(5)
for i in range(2):
driver.find_element_by_xpath('//div[@class="more hidden"]').click()
sleep(8)
sleep(8)
element = driver.find_element_by_id("root").get_attribute('innerHTML')
#driver.execute_script("return document.getElementsByTagName('html')[0].innerHTML")
#WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, 'more hidden')))
'''
results = html.find_elements_by_xpath('//div[@class="name"]')
# wait for the page to load
for result in results:
startup = result.find_elements_by_xpath('.//a')
link = startup.get_attribute('href')
print(link)
'''
page_source = driver.page_source
html = BeautifulSoup(element, 'html.parser')
#for link in html.findAll('a', {'class': 'startup-link'}):
# print link
divs = html.find_all("div", class_=" dts27 frw44 _a _jm")
在我添加关注者点击事件之前,上面的代码正在运行,并为我提供了 html 源代码。
我的最终目标是将所有这五个信息,如公司名称、链接、加入日期、关注者数量和公司描述(在访问他们的个人链接后获得)到 CSV 或 xls 文件中。
感谢帮助和 cmets。 这是我的第一个 python 工作和 selenium,所以有点困惑,需要指导。
谢谢:-)
【问题讨论】:
标签: python html-parsing selenium-chromedriver