Python Beautiful Soup修改onclick事件属性

时间:2017-03-13 12:04:05

标签: html python-2.7 beautifulsoup

我正在尝试修改.html页面中按钮的行为,更确切地说,我有:

<input id="next" name="signIn" class="rc-button rc-button-submit" type="submit" value="Next"> 

我想获得:

<input id="next" name="signIn" class="rc-button rc-button-submit"  value="NEXT" Onclick="window.location.href='http://www.some_web_page.com/'">

使用bs4可以做到这一点吗?到目前为止,我设法识别html页面中所需的部分,但我不确定如何才能进行更改:

page=requests.get('https://www.some_page.com/') soup = BeautifulSoup(page.content, 'html.parser') print (soup.findAll(id="next"))

由于

1 个答案:

答案 0 :(得分:0)

In [44]: html = '''<input id="next" name="signIn" class="rc-button rc-button-submit" type="submit" value="Next">'''

In [45]: soup = BeautifulSoup(html, 'lxml')

In [46]: soup.input['Onclick'] = "window.location.href='http://www.some_web_page.com/'"

In [47]: soup
Out[47]: <html><body><input Onclick="window.location.href='http://www.some_web_page.com/'" class="rc-button rc-button-submit" id="next" name="signIn" type="submit" value="Next"/></body></html>