Python爬取网站链接和标题:以cnboling.cn为例
使用Python的requests和BeautifulSoup库可以轻松地从网页中提取所需的信息,例如链接和标题。
以下代码展示如何从网站'https://www.cnboling.cn/3g/list.php?fid=55'中提取'div class='tuiguang''下的所有链接和标题内容:
import requests
from bs4 import BeautifulSoup
url = 'https://www.cnboling.cn/3g/list.php?fid=55'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
tuiguang = soup.find('div', {'class': 'tuiguang'})
links = tuiguang.find_all('a')
for link in links:
title = link.text
href = link['href']
print(title, href)
输出结果:
招聘-业务员-会计-报价-运营 -美工-文员 bencandy.php?city_id=1&fid=55&id=1515090
铂亚丝网长年招聘优秀业务员、短视频拍摄 bencandy.php?city_id=1&fid=57&id=1554319
的男女 bencandy.php?city_id=1&fid=57&id=1554319
代码说明:
- 导入requests和BeautifulSoup库。
- 使用requests库获取目标网页内容。
- 使用BeautifulSoup库解析HTML内容。
- 找到包含所有链接的'div'元素,并提取所有'a'标签。
- 循环遍历每个'a'标签,获取链接文本和链接地址,并打印输出。
通过以上代码,我们可以轻松地从网页中提取所需的信息,并进行进一步的处理和分析。
原文地址: https://www.cveoy.top/t/topic/mW3y 著作权归作者所有。请勿转载和采集!