当前位置: 首页> 健康> 科研 > 动画设计就业方向和发展前景_简历制作哪里好_企业网站seo方案_深圳搜索seo优化排名

动画设计就业方向和发展前景_简历制作哪里好_企业网站seo方案_深圳搜索seo优化排名

时间:2025/8/12 0:36:26来源:https://blog.csdn.net/Sr6220033/article/details/146383834 浏览次数:0次
动画设计就业方向和发展前景_简历制作哪里好_企业网站seo方案_深圳搜索seo优化排名

基础版本

爬取网页后直接将前端html代码不加处理的输出

# pip3 install requests
import requests# request the target URL
def crawler():response = requests.get("https://www.scrapingcourse.com/ecommerce/")response.raise_for_status()print(response.text)# execute the crawler
crawler()

无限增生的爬虫

从第一个链接开始,记录已经遍历过的链接;
并且从这个链接爬取的html代码中记录 a[href] 的链接,存储到将要遍历的列表;
对于已经爬取的链接,直接continue处理

# pip3 install requests
import requestsdef crawler():while urls_to_visit:# get the page to visit from the listcurrent_url = urls_to_visit.pop(0)print(current_url)if current_url in visited_urls:continue# 记录访问过的url到列表中visited_urls.add(current_url)try:response = requests.get(current_url, timeout=5)  # 设置超时时间,避免死循环response.raise_for_status()  # 检查请求是否成功except requests.RequestException as e:print(f"请求失败: {current_url}, 错误: {e}")continue# parse the HTMLsoup = BeautifulSoup(response.text, "html.parser")# collect all the linkslink_elements = soup.select("a[href]")for link_element in link_elements:url = link_element["href"]if url.startswith("#"):continue  # ignore internal links# convert links to absolute URLsif not url.startswith("http"):absolute_url = requests.compat.urljoin(target_url, url)else:absolute_url = url# ensure the crawled link belongs to the target domain and hasn't been visitedif (absolute_url.startswith(target_url)and absolute_url not in urls_to_visit):urls_to_visit.append(url)# pip3 install requests beautifulsoup4from bs4 import BeautifulSouptarget_url = "https://www.scrapingcourse.com/ecommerce/"
# initialize the list of discovered URLs
urls_to_visit = [target_url]
visited_urls = set()  # 记录已访问的 URL,防止重复爬取
# execute the crawler
crawler()

无限增生的效果

在这里插入图片描述
部分链接爬取失败后会返回错误信息

关键字:动画设计就业方向和发展前景_简历制作哪里好_企业网站seo方案_深圳搜索seo优化排名

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: