2 回答

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超8個(gè)贊
以下
from bs4 import BeautifulSoup
html = '''<td class="tdAction">
<div class="formResponseBtn icon-only">
<a href="#fh" onclick="javascript: openPopUpFullScreen('/esop/toolkit/negotiation/rfq/publicRfqSummaryReport.do?rfqId=rfq_229969', '');" class="openNewPage" title="Open a new window to view > View or download a Summary of this PQQ/ITT which includes details of the PQQ/ITT settings, format and questions">
<img src="/esop_custom/images/buttons/print_button.png" title="Open a new window to view > View or download a Summary of this PQQ/ITT which includes details of the PQQ/ITT settings, format and questions" alt="Open a new window to view > View or download a Summary of this PQQ/ITT which includes details of the PQQ/ITT settings, format and questions"><img src="/esop_custom/images/buttons/openNewWindow_button.png" title="(Opens in new window)" alt="(Opens in new window)">
</a>
</div>
</td>'''
soup = BeautifulSoup(html, features="lxml")
a = soup.find('a')
onclick = a.attrs['onclick']
left = onclick.find("'")
right = onclick.find("'",left+1)
print('URL is: {}'.format(onclick[left+1:right]))
輸出
URL is: /esop/toolkit/negotiation/rfq/publicRfqSummaryReport.do?rfqId=rfq_229969

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超4個(gè)贊
對(duì)于 get_attribute:
我認(rèn)為您在沒有“onclick”屬性的情況下得到了錯(cuò)誤的元素。
您應(yīng)該擴(kuò)展 css 選擇器并確認(rèn)它只會(huì)找到一個(gè)元素。
對(duì)于 current_url:
您應(yīng)該先切換到新窗口。嘗試使用以下代碼:
# window_handles[-1] refer to last window created.
browser.switch_to.window(browser.window_handles[-1])
print(browser.current_url)
添加回答
舉報(bào)