簡體   English   中英

使用 selenium 和 beautifulsoup 進行網頁抓取 .. 解析和選擇按鈕時遇到麻煩

[英]Web scraping using selenium and beautifulsoup.. trouble in parsing and selecting button

我正在嘗試瀏覽以下網站“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")

在我添加 Followers 點擊事件之前,上面的代碼正在工作並且給了我 html 源代碼。

我的最終目標是將所有這五個信息導入到 CSV 或 xls 文件中,例如公司名稱、鏈接、加入日期、關注者數量和公司描述(在訪問他們的各個鏈接后獲得)。

幫助和評論表示贊賞。 這是我的第一個 python 工作和 selenium,所以有點困惑,需要指導。

謝謝:-)

click方法用於模擬鼠標點擊; 它用於可單擊的元素,例如按鈕、下拉列表、復選框等。您已將此方法應用於不可單擊的div元素。 諸如divspanframe等元素用於組織 HTML 並提供字體裝飾等。

要使此代碼起作用,您需要確定頁面中實際可點擊的元素。

哎呀,我的打字錯誤或這里出現了一些愚蠢的錯誤,我錯誤地使用了 div 類名稱,它是“列關注者可排序”,而不是我使用的是“列關注者可排序選擇”。 :-( 現在上面的工作非常好..但是有人可以用beautifulsoup HTML解析部分指導我嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM