用BeautifulSoup解析获取a标签里的网址该如何写?

Dai_bhid 2017-04-26 03:01:16
<tr class="bg">
<td class="td-title faceblue">
<span class="face" title="普通帖">

</span>
<a href="/post-basketball-200125-1.shtml" target="_blank">
当教练的最高境界——让对手任谁都能打出神仙球!
</a>
</td>
<td><a href="http://www.tianya.cn/75944044" target="_blank" class="author">司马取印</a></td>
<td>4420</td>
<td>163</td>
<td title="2017-04-25 23:44">04-25 23:44</td>
</tr>

<tr>
<td class="td-title faceblue">
<span class="face" title="普通帖">

</span>
<a href="/post-basketball-200496-1.shtml" target="_blank">
10年的黑色乔丹6代!!!(转载)<span class="art-ico art-ico-3" title="内有2张图片"></span>
</a>
</td>
<td><a href="http://www.tianya.cn/126744501" target="_blank" class="author">13141373133</a></td>
<td>102</td>
<td>9</td>
<td title="2017-04-25 17:44">04-25 17:44</td>
</tr>

如上HTML文档,我用BeautifulSoup解析后想获取<a>标签里的网址例如:/post-basketball-200496-1.shtml,类似于上述的文档有多个,想把所有的网址获取下来改怎样写?在线等,挺急的。。。
...全文
8451 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
ac不知深 2020-02-24
  • 打赏
  • 举报
回复
引用 3 楼 sanGuo_uu 的回复:
这样子可以了
# -*- coding:utf-8 -*-

html="""
"""

from bs4 import BeautifulSoup
soup=BeautifulSoup(html,'lxml')
zzr=soup.find_all('a')
for item in zzr:
	print item.get("href")
假如我只想输出第二个a标签里的网址,需要怎办呢?在循环里加上一个if条件吗?
ac不知深 2020-02-24
  • 打赏
  • 举报
回复
引用 16 楼 sanGuo_uu 的回复:
[quote=引用 15 楼 ac不知深 的回复:] [quote=引用 3 楼 sanGuo_uu 的回复:] 这样子可以了
# -*- coding:utf-8 -*-

html="""
"""

from bs4 import BeautifulSoup
soup=BeautifulSoup(html,'lxml')
zzr=soup.find_all('a')
for item in zzr:
	print item.get("href")
假如我只想输出第二个a标签里的网址,需要怎办呢?在循环里加上一个if条件吗?[/quote] 我现在已经不搞这个了。 你要加if也可以实现你的需求。 你可以看看find_all返回的是什么类型, 比如说是数组的话,你检查下数组长度,直接取第二个就可以了 [/quote] 非常感谢,使用if语句已经得到想要的结果了,谢谢
sanGuo_uu 2020-02-24
  • 打赏
  • 举报
回复
引用 15 楼 ac不知深 的回复:
[quote=引用 3 楼 sanGuo_uu 的回复:] 这样子可以了
# -*- coding:utf-8 -*-

html="""
"""

from bs4 import BeautifulSoup
soup=BeautifulSoup(html,'lxml')
zzr=soup.find_all('a')
for item in zzr:
	print item.get("href")
假如我只想输出第二个a标签里的网址,需要怎办呢?在循环里加上一个if条件吗?[/quote] 我现在已经不搞这个了。 你要加if也可以实现你的需求。 你可以看看find_all返回的是什么类型, 比如说是数组的话,你检查下数组长度,直接取第二个就可以了
CDSoftwareWj 2017-06-21
  • 打赏
  • 举报
回复
反b4 qq_35915910
CDSoftwareWj 2017-06-21
  • 打赏
  • 举报
回复
u012536120 人家最少从另一个面解决了问题
sanGuo_uu 2017-04-26
  • 打赏
  • 举报
回复
引用 5 楼 qq_35915910 的回复:
哪里要上面那么难 # -*- coding:utf-8 -*- html=""" """ from bs4 import BeautifulSoup soup=BeautifulSoup(html,'lxml') zzr=soup.find_all('a') for a in zzr: print a["href"] bs 可以直接yong[]拿某一条属性
你答你的题,诋毁我的代码干什么
Dai_bhid 2017-04-26
  • 打赏
  • 举报
回复
引用 10 楼 FengHuaJianShi 的回复:
[quote=引用 7 楼 Dai_bhid 的回复:] [quote=引用 5 楼 qq_35915910 的回复:] 哪里要上面那么难 # -*- coding:utf-8 -*- html=""" """ from bs4 import BeautifulSoup soup=BeautifulSoup(html,'lxml') zzr=soup.find_all('a') for a in zzr: print a["href"] bs 可以直接yong[]拿某一条属性
但是我要多个呐,class="td-title faceblue"这个类里面的href网址哦[/quote]
引用 7 楼 Dai_bhid 的回复:
[quote=引用 5 楼 qq_35915910 的回复:] 哪里要上面那么难 # -*- coding:utf-8 -*- html=""" """ from bs4 import BeautifulSoup soup=BeautifulSoup(html,'lxml') zzr=soup.find_all('a') for a in zzr: print a["href"] bs 可以直接yong[]拿某一条属性
但是我要多个呐,class="td-title faceblue"这个类里面的href网址哦[/quote]
from bs4 import BeautifulSoup
soup = BeautifulSoup(html)
tds = soup.find_all('td',class_ = 'td-title faceblue')
for td in tds:
    zzr = td.find_all('a')
    for a in zzr:
        print(a["href"])
find_all时增加个 class_ 参数[/quote] 谢谢
Dai_bhid 2017-04-26
  • 打赏
  • 举报
回复
引用 8 楼 u012536120 的回复:
我再写了一层
# -*- coding:utf-8 -*-

html="""
"""
from bs4 import BeautifulSoup
soup=BeautifulSoup(html,'lxml')
zzr=soup.find_all('td',class_="td-title faceblue")
for item in zzr:
    list_tmp=item.find_all('a')
    for a in list_tmp:
    	print a.get('href')
可以了,谢谢!
风华渐逝 2017-04-26
  • 打赏
  • 举报
回复
引用 7 楼 Dai_bhid 的回复:
[quote=引用 5 楼 qq_35915910 的回复:] 哪里要上面那么难 # -*- coding:utf-8 -*- html=""" """ from bs4 import BeautifulSoup soup=BeautifulSoup(html,'lxml') zzr=soup.find_all('a') for a in zzr: print a["href"] bs 可以直接yong[]拿某一条属性
但是我要多个呐,class="td-title faceblue"这个类里面的href网址哦[/quote]
引用 7 楼 Dai_bhid 的回复:
[quote=引用 5 楼 qq_35915910 的回复:] 哪里要上面那么难 # -*- coding:utf-8 -*- html=""" """ from bs4 import BeautifulSoup soup=BeautifulSoup(html,'lxml') zzr=soup.find_all('a') for a in zzr: print a["href"] bs 可以直接yong[]拿某一条属性
但是我要多个呐,class="td-title faceblue"这个类里面的href网址哦[/quote]
from bs4 import BeautifulSoup
soup = BeautifulSoup(html)
tds = soup.find_all('td',class_ = 'td-title faceblue')
for td in tds:
    zzr = td.find_all('a')
    for a in zzr:
        print(a["href"])
find_all时增加个 class_ 参数
sanGuo_uu 2017-04-26
  • 打赏
  • 举报
回复
我再写了一层
# -*- coding:utf-8 -*-

html="""
"""
from bs4 import BeautifulSoup
soup=BeautifulSoup(html,'lxml')
zzr=soup.find_all('td',class_="td-title faceblue")
for item in zzr:
    list_tmp=item.find_all('a')
    for a in list_tmp:
    	print a.get('href')
Dai_bhid 2017-04-26
  • 打赏
  • 举报
回复
引用 5 楼 qq_35915910 的回复:
哪里要上面那么难 # -*- coding:utf-8 -*- html=""" """ from bs4 import BeautifulSoup soup=BeautifulSoup(html,'lxml') zzr=soup.find_all('a') for a in zzr: print a["href"] bs 可以直接yong[]拿某一条属性
但是我要多个呐,class="td-title faceblue"这个类里面的href网址哦
Dai_bhid 2017-04-26
  • 打赏
  • 举报
回复
引用 3 楼 u012536120 的回复:
这样子可以了
# -*- coding:utf-8 -*-

html="""
"""

from bs4 import BeautifulSoup
soup=BeautifulSoup(html,'lxml')
zzr=soup.find_all('a')
for item in zzr:
	print item.get("href")
如果多加一个条件,必须是class="td-title faceblue"这个类里面的网址呢,该如何写?
杂技猫 2017-04-26
  • 打赏
  • 举报
回复
哪里要上面那么难 # -*- coding:utf-8 -*- html=""" """ from bs4 import BeautifulSoup soup=BeautifulSoup(html,'lxml') zzr=soup.find_all('a') for a in zzr: print a["href"] bs 可以直接yong[]拿某一条属性
Dai_bhid 2017-04-26
  • 打赏
  • 举报
回复
引用 3 楼 u012536120 的回复:
这样子可以了
# -*- coding:utf-8 -*-

html="""
"""

from bs4 import BeautifulSoup
soup=BeautifulSoup(html,'lxml')
zzr=soup.find_all('a')
for item in zzr:
	print item.get("href")
谢谢,我正则不太熟,不过你的挺好用,非常感谢!
sanGuo_uu 2017-04-26
  • 打赏
  • 举报
回复
这样子可以了
# -*- coding:utf-8 -*-

html="""
"""

from bs4 import BeautifulSoup
soup=BeautifulSoup(html,'lxml')
zzr=soup.find_all('a')
for item in zzr:
	print item.get("href")
sanGuo_uu 2017-04-26
  • 打赏
  • 举报
回复
用正则不开心么?
# -*- coding:utf-8 -*-

html="""
<tr class="bg">	
<td class="td-title faceblue">
<span class="face" title="普通帖">

</span>
<a href="/post-basketball-200125-1.shtml" target="_blank">
当教练的最高境界——让对手任谁都能打出神仙球!
</a>
</td> 
<td><a href="http://www.tianya.cn/75944044" target="_blank" class="author">司马取印</a></td>
<td>4420</td>
<td>163</td>
<td title="2017-04-25 23:44">04-25 23:44</td>
</tr>

<tr>	
<td class="td-title faceblue">
<span class="face" title="普通帖">

</span>
<a href="/post-basketball-200496-1.shtml" target="_blank">
10年的黑色乔丹6代!!!(转载)<span class="art-ico art-ico-3" title="内有2张图片"></span>
</a>
</td> 
<td><a href="http://www.tianya.cn/126744501" target="_blank" class="author">13141373133</a></td>
<td>102</td>
<td>9</td>
<td title="2017-04-25 17:44">04-25 17:44</td>
</tr>
"""

"""
from bs4 import BeautifulSoup
soup=BeautifulSoup(html,'lxml')
zzr=soup.find_all('a')
print zzr
"""

import re

patt=re.compile(r'<a.*?href="(.*?)"',re.S)
zzr=patt.findall(html)
print zzr
Dai_bhid 2017-04-26
  • 打赏
  • 举报
回复
all_a=BeautifulSoup(html,'html.parser').find('td',class_="td-title faceblue").a['href'] 这样可以爬下来,但是只能爬一个,我想全部爬下来,如果改成find_all就会报错:AttributeError: 'ResultSet' object has no attribute 'a'

37,718

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧