Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中使用BeautifulSoup从href提取部分文本_Python_Beautifulsoup - Fatal编程技术网

如何在Python中使用BeautifulSoup从href提取部分文本

如何在Python中使用BeautifulSoup从href提取部分文本,python,beautifulsoup,Python,Beautifulsoup,这是我的密码: 对于数据中的项目: print(item.find_all('td')[2].find('a')) print(item.find('span').text.strip()) print(item.find_all('td')[3].text) print(item.find_all('td')[2].find(target="_blank").string.strip()) 它在下面打印此文本 <a href="argument_transcripts/2016/16-

这是我的密码:

对于数据中的项目:

print(item.find_all('td')[2].find('a'))
print(item.find('span').text.strip())
print(item.find_all('td')[3].text)
print(item.find_all('td')[2].find(target="_blank").string.strip())
它在下面打印此文本

<a href="argument_transcripts/2016/16-399_3f14.pdf" 
id="ctl00_ctl00_MainEditable_mainContent_rptTranscript_ctl01_hypFile" 
target="_blank">16-399. </a>

Perry v. Merit Systems Protection Bd.

04/17/17

16-399.

佩里五世。功绩系统保护。
04/17/17
16-399.
我只想从href标签中看到这一部分:
16-399_3f14


我该怎么做?谢谢。

您可以使用find_all来提取具有href属性的锚元素,然后解析href值以获取您要查找的信息

from BeautifulSoup import BeautifulSoup

html = '''<a href="argument_transcripts/2016/16-399_3f14.pdf" 
id="ctl00_ctl00_MainEditable_mainContent_rptTranscript_ctl01_hypFile" 
target="_blank">16-399. </a>'''

soup = BeautifulSoup(html)

for a in soup.find_all('a', href=True):
    url = a['href'].split('/')
    print url[-1]
16-399_3f14.pdf

你试过什么样的东西
re
模块提供了从字符串中提取子字符串的强大工具,但是这种情况非常简单,您可能只需调用几个
str.split