BeautifulSoup爬取指定类div标签下的网址href

测试案例:
在这里插入图片描述
1.select方法

    for item in soup.select('div[class="f-l intern-detail__job"] p a'):
        detail_url = item.get('href')
        print(detail_url)

2.find_all 方法

	for items in soup.find_all('div',class_='f-l intern-detail__job'):
    	item = items.select('p a')[0]
    	detail_url = item.get('href')
    	print(detail_url)

BeautifulSoup是一个用于解析HTML和XML文档的Python库,常用于网络爬虫开发。它提供了一些简单的接口来提取和操作网页数据。以下是BeautifulSoup的一些主要特点和用法: 1. 安装: 首先需要安装BeautifulSoup库,可以使用pip命令: ``` pip install beautifulsoup4 ``` 2. 解析HTML文档: ```python from bs4 import BeautifulSoup html_doc = "<html><head><title>Test Page</title></head><body><p>Hello, world!</p></body></html>" soup = BeautifulSoup(html_doc, 'html.parser') ``` 3. 提取数据: ```python # 获取标题 title = soup.title.string print(title) # 获取所有段落 paragraphs = soup.find_all('p') for p in paragraphs: print(p.text) ``` 4. 查找元素: ```python # 通过标签名查找 divs = soup.find_all('div') # 通过id查找 elem = soup.find(id='element-id') # 通过名查找 elems = soup.find_all(class_='class-name') ``` 5. 导航文档树: ```python # 获取父节点 parent = elem.parent # 获取子节点 children = elem.children # 获取兄弟节点 siblings = elem.next_siblings ``` 6. 修改文档: ```python # 修改文本 elem.string = "New Text" # 添加新标签 new_tag = soup.new_tag('a', href='https://example.com') elem.append(new_tag) # 删除标签 elem.decompose() ``` BeautifulSoup的优势在于它能够处理不规范或复杂的HTML文档,并且提供了简单易用的API,使得数据提取变得更加直观和高效。然而,对于大型网站或需要频繁访问的网页,建议结合使用requests库进行HTTP请求,并考虑使用更高效的解析器如lxml,以提升性能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值