Python phantomjs加载网页不正确

Python phantomjs加载网页不正确,python,selenium-webdriver,phantomjs,Python,Selenium Webdriver,Phantomjs,我有一个问题,从这个链接提取 从主页本身的链接中获取数据。 知道为什么会这样吗? 我用PhantomJS硒和美丽的汤来帮助我 # The standard library modules import os import sys import re import sqlite3 import locale # The wget module import wget import time import calendar from datetime import datetime # The

我有一个问题,从这个链接提取

从主页本身的链接中获取数据。

知道为什么会这样吗? 我用PhantomJS硒和美丽的汤来帮助我

# The standard library modules
import os
import sys
import re
import sqlite3
import locale
# The wget module
import wget
import time
import calendar
from datetime import datetime
# 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 getURLS(url):
    driver = webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true'])
    driver.get(url) # load the web page
    src = driver.page_source
    #Get text and split it
    soup = BeautifulSoup(src, 'html5lib')

    print soup

link ='http://www.bursamalaysia.com/market/listed-companies/company-announcements/#/?category=FA&sub_category=FA1&alphabetical=All&company=5250'
getURLS(link)
来自Alex Lucaci的解决方案

def getURLS(url):
    driver = webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true'])
    driver.get(url) # load the web page
    src = driver.page_source
    category_select = Select(driver.find_element_by_xpath('//*[@id="bm_announcement_types"]'))
    category_select.select_by_visible_text("Financial Results")
    category_select2 = Select(driver.find_element_by_xpath('//*[@id="bm_sub_announcement_types"]'))
    category_select2.select_by_visible_text("Financial Results")
    category_select3 = Select(driver.find_element_by_xpath('//*[@id="bm_company_list"]'))
    category_select3.select_by_visible_text("7-ELEVEN MALAYSIA HOLDINGS BERHAD (5250)")
    driver.find_element_by_xpath('//*[@id="bm_company_announcements_search_form"]/input[1]').click()
    src = driver.page_source
    soup = BeautifulSoup(src, 'html5lib')
    link="http://www.bursamalaysia.com/market/listed-companies/company-announcements/#/?category=all"
    getURLS(link)

保存源代码时,页面未完全加载您提交的文章,因此请尝试等待几秒钟,然后再获取页面源代码:

def getURLS(url):
driver = webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true'])
driver.get(url) # load the web page
time.sleep(5)# waiting for 5 seconds before fetching the source
src = driver.page_source
#Get text and split it
soup = BeautifulSoup(src, 'html5lib')

print soup
要执行下拉选择,您必须按如下方式导入
选择
类:
从selenium.webdriver.support.ui导入选择
,然后您必须像这样选择下拉元素:

category_select = Select(driver.find_element_by_xpath('//*[@id="bm_announcement_types"]'))
category_select.select_by_visible_text('Financial Results')
在我的示例中,我为-Category-dropdown做了这项工作,按照每个类别的确切步骤进行操作。 请注意,按xpath选择下拉列表是最好的方法,您可以通过使用Google Chrome->righ单击元素->检查->右键单击右菜单中出现的->复制->复制xpath中的
来实现这一点

当您选择了所有元素后,您必须单击Submit并等待几秒钟加载,然后您将获取源代码


如果我的回答对你有帮助,请告诉我。

嘿,对不起,伙计。我测试了一下,把汤打印出来。它仍然显示主页上的数据,而不是过滤后的值。有没有办法让它通过它的下拉选择值代替?谢谢伙计!在谷歌上搜索了一下如何点击submit,你在我身上通过xpath找到了查找元素的新知识,这很有效!非常感谢你!