python获取html中js,如何通过Selenium / Python获取JavaScript编写的html内容[复制]

参见英文答案 >

Get HTML Source of WebElement in Selenium WebDriver using Python                                    13个

我正在使用Selenium进行网络爬行,我希望在Selenium模拟点击虚假链接后获得由JavaScript编写的元素(例如链接).

我尝试了get_html_source(),但它不包含JavaScript编写的内容.

我编写的代码:

def test_comment_url_fetch(self):

sel = self.selenium

sel.open("/rmrb")

url = sel.get_location()

#print url

if url.startswith('http://login'):

sel.open("/rmrb")

i = 1

while True:

try:

if i == 1:

sel.click("//div[@class='WB_feed_type SW_fun S_line2']/div/div/div[3]/div/a[4]")

print "click"

else:

XPath = "//div[@class='WB_feed_type SW_fun S_line2'][%d]/div/div/div[3]/div/a[4]"%i

sel.click(XPath)

print "click"

except Exception, e:

print e

break

i += 1

html = sel.get_html_source()

html_file = open("tmp\\foo.html", 'w')

html_file.write(html.encode('utf-8'))

html_file.close()

我使用while循环来点击一系列虚假链接,触发js-actions来显示额外的内容,而这些内容就是我想要的.但是sel.get_html_source()没有给出我想要的东西.

有人可以帮忙吗?非常感谢.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值