通过 table/list 内的锚文本查找 href
Finding href by anchor text inside table/list
我正在尝试使用 Python bs4 从我之前成功登录(使用请求)的网站中提取带有特定锚文本的 href。
这是着陆页的伪HTML:
<table class="submissions">
<thead>some thead</thead>
<tbody><tr class="active">
<th scope="row">uninterestingtext</th>
<td>uninterestingtext</td><td></td>
</tr>
<tr class="active">
<th scope="row">uninteresting</th>
<td>uninteresting text</td><td></td></tr>
<tr class="lastrow active"><th scope="row">uninteresting</th>
<td>uninteresting text</td>
<td></td>
</tr>
<tr class="lastrow inactive">
<th scope="row">uninteresting text</th>
<td>uninterestingtext
<ul>
<li><a href="uninteresting_href">someLink</a> </li>
<li><a href="uninteresting_href">someLink</a> </li>
<li><a href=**InterestingLink**>**Upload...**</a></li>
</ul>
</td>
</tr></tbody></table>
现在我试图通过在 'a' 标签之间查找 Upload... 文本来提取 InterestingLink。
这是我尝试过的:
landing_page_soup = BeautifulSoup(*responseFromSuccessfulLogin*.text, 'html.parser')
important_page = landing_page_soup.find('a',{'href':True,'text':'Upload...'}).get('href')
但这总是会引发错误
AttributeError: 'NoneType' object has no attribute 'get'
因为 "important_page" 总是 "None"。
注意:我已确定“responseFromSuccessfulLogin.text”是正确的 HTML,其中包含所需的链接。
在阅读了关于类似问题的其他论坛帖子后,我修改了该行以使用方法 'select' 来查询 css-选择器以及方法 'findAll' 但没有成功。
我觉得我搞砸了,因为它是一个 table,链接在里面。
BeautifulSoup 接受可调用对象。
html = BeautifulSoup(response.content, 'html.parser')
important_page = html.findAll('a', href=True, text=lambda i: i if 'Upload...' in i else False)
print(important_page[0]['href'])
(代表OP发布解决方案).
这个:
important_page = landing_page_soup.find('a', title='Upload...')['href']
非常适合我。我只得到我想要的link。
我正在尝试使用 Python bs4 从我之前成功登录(使用请求)的网站中提取带有特定锚文本的 href。
这是着陆页的伪HTML:
<table class="submissions">
<thead>some thead</thead>
<tbody><tr class="active">
<th scope="row">uninterestingtext</th>
<td>uninterestingtext</td><td></td>
</tr>
<tr class="active">
<th scope="row">uninteresting</th>
<td>uninteresting text</td><td></td></tr>
<tr class="lastrow active"><th scope="row">uninteresting</th>
<td>uninteresting text</td>
<td></td>
</tr>
<tr class="lastrow inactive">
<th scope="row">uninteresting text</th>
<td>uninterestingtext
<ul>
<li><a href="uninteresting_href">someLink</a> </li>
<li><a href="uninteresting_href">someLink</a> </li>
<li><a href=**InterestingLink**>**Upload...**</a></li>
</ul>
</td>
</tr></tbody></table>
现在我试图通过在 'a' 标签之间查找 Upload... 文本来提取 InterestingLink。
这是我尝试过的:
landing_page_soup = BeautifulSoup(*responseFromSuccessfulLogin*.text, 'html.parser')
important_page = landing_page_soup.find('a',{'href':True,'text':'Upload...'}).get('href')
但这总是会引发错误
AttributeError: 'NoneType' object has no attribute 'get'
因为 "important_page" 总是 "None"。
注意:我已确定“responseFromSuccessfulLogin.text”是正确的 HTML,其中包含所需的链接。
在阅读了关于类似问题的其他论坛帖子后,我修改了该行以使用方法 'select' 来查询 css-选择器以及方法 'findAll' 但没有成功。
我觉得我搞砸了,因为它是一个 table,链接在里面。
BeautifulSoup 接受可调用对象。
html = BeautifulSoup(response.content, 'html.parser')
important_page = html.findAll('a', href=True, text=lambda i: i if 'Upload...' in i else False)
print(important_page[0]['href'])
(代表OP发布解决方案).
这个:
important_page = landing_page_soup.find('a', title='Upload...')['href']
非常适合我。我只得到我想要的link。