python爬虫获取a标签属性值
时间: 2023-08-23 13:14:08 浏览: 130
要获取a标签的属性值,可以使用BeautifulSoup对象的find_all方法来找到所有的a标签,然后通过遍历这些a标签,使用attrs属性来获取每个a标签的属性字典,再通过属性名来获取属性值。具体的代码如下所示:
```python
a_tags = soup.find_all('a') # 找到所有的a标签
for a_tag in a_tags:
href = a_tag.attrs.get('href') # 获取href属性值
print(href)
```
这段代码会打印出所有a标签的href属性值。
#### 引用[.reference_title]
- *1* *2* *3* [Python爬虫入门8:BeautifulSoup获取html标签相关属性](https://blog.csdn.net/LaoYuanPython/article/details/113413766)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]