beautifulsoup无法提取href链接

问题描述 投票:0回答:2

所以我使用selenium,phantomjs作为我的webdriver,以及beautifulsoup。目前我想提取属性标题下的所有链接。 The site i want to extract

但是,似乎根本没有拿起这些链接!到底是怎么回事 ?

# The standard library modules
import os
import sys
import re

# The wget module
import wget

# The BeautifulSoup module
from bs4 import BeautifulSoup

# The selenium module
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By


def getListLinks(link):
    #setup drivers
    driver = webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true'])
    driver.get(link) # load the web page
    src = driver.page_source 

    #Get text and split it
    soup = BeautifulSoup(src, 'html5lib')
    print soup
    links = soup.find_all('a')
    print links    

    driver.close()

getListLinks("http://www.bursamalaysia.com/market/listed-companies/company-announcements/#/?category=FA&sub_category=FA1&alphabetical=All&company=9695&date_from=01/01/2012&date_to=31/12/2016")

这是我想要提取的链接的示例

<a href="/market/listed-companies/company-announcements/5455245">Quarterly rpt on consolidated results for the financial period ended 31/03/2017</a>
python selenium web-scraping beautifulsoup phantomjs
2个回答
2
投票

我真正不明白的是为什么你要将美丽的硒与硒混合。 Selenium拥有它自己的api for extract dom元素。您不需要将BS4带入图片中。除了BS4只能使用静态HTML并忽略动态生成的HTML,你的selenium实例能够处理。

做就是了

driver.find_element_by_tag_name('a')

0
投票

您想要标题列下的链接,这是表的第4列。您可以使用nth-of-type选择器限制目标表每行的4列内的表格单元格(td元素)。添加等待条件以确保元素存在。

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

d = webdriver.Chrome()
url = 'http://www.bursamalaysia.com/market/listed-companies/company-announcements/#/?category=all'
d.get(url)
links = [link.get_attribute('href') for link in WebDriverWait(d, 10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, 'tr  td:nth-of-type(4) a')))]
print(links)
d.quit()
© www.soinside.com 2019 - 2024. All rights reserved.