当前位置: 首页 > news >正文

Python 爬虫基础

Python 爬虫基础

1.1 理论

在浏览器通过网页拼接【/robots.txt】来了解可爬取的网页路径范围

例如访问: https://www.csdn.net/robots.txt

User-agent: *
Disallow: /scripts
Disallow: /public
Disallow: /css/
Disallow: /images/
Disallow: /content/
Disallow: /ui/
Disallow: /js/
Disallow: /scripts/
Disallow: /article_preview.html*
Disallow: /tag/
Disallow: /?
Disallow: /link/
Disallow: /tags/
Disallow: /news/
Disallow: /xuexi/

通过Python Requests 库发送HTTP【Hypertext Transfer Protocol “超文本传输协议”】请求

通过Python Beautiful Soup 库来解析获取到的HTML内容

HTTP请求

在这里插入图片描述

HTTP响应

在这里插入图片描述

1.2 实践代码 【获取价格&书名】

import requests
# 解析HTML
from bs4 import BeautifulSoup# 将程序伪装成浏览器请求
head = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
}
requests = requests.get("http://books.toscrape.com/",headers= head)
# 指定编码
# requests.encoding= 'gbk'
if requests.ok:# file = open(r'C:\Users\root\Desktop\Bug.html', 'w')# file.write(requests.text)# file.closecontent =  requests.text## html.parser 指定当前解析 HTML 元素soup = BeautifulSoup(content, "html.parser")## 获取价格all_prices = soup.findAll("p", attrs={"class":"price_color"})for price in all_prices:print(price.string[2:])## 获取名称all_title = soup.findAll("h3")for title in all_title:## 获取h3下面的第一个a元素print(title.find("a").string)
else:print(requests.status_code)

1.3 实践代码 【获取 Top250 的电影名】

import requests
# 解析HTML
from bs4 import BeautifulSoup
head = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
}
# 获取 TOP 250个电影名
for i in range(0,250,25):response = requests.get(f"https://movie.douban.com/top250?start={i}", headers= head)if response.ok:content =  response.textsoup = BeautifulSoup(content, "html.parser")all_titles = soup.findAll("span", attrs={"class": "title"})for title in all_titles:if "/" not in title.string:print(title.string) else:print(response.status_code)

1.4 实践代码 【下载图片】

import requests
# 解析HTML
from bs4 import BeautifulSouphead = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
}
response = requests.get(f"https://www.maoyan.com/", headers= head)
if response.ok:soup = BeautifulSoup(response.text, "html.parser")for img in soup.findAll("img", attrs={"class": "movie-poster-img"}):img_url = img.get('data-src')alt = img.get('alt')path = 'img/' + alt + '.jpg'res = requests.get(img_url)with open(path, 'wb') as f:f.write(res.content)
else:print(response.status_code)

1.5 实践代码 【千图网图片 - 爬取 - 下载图片】

import requests
# 解析HTML
from bs4 import BeautifulSoup# 千图网图片 - 爬取
head = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
}
# response = requests.get(f"https://www.58pic.com/piccate/53-0-0.html", headers= head)
# response = requests.get(f"https://www.58pic.com/piccate/53-598-2544.html", headers= head)
response = requests.get(f"https://www.58pic.com/piccate/53-527-1825.html", headers= head)
if response.ok:soup = BeautifulSoup(response.text, "html.parser")for img in soup.findAll("img", attrs={"class": "lazy"}):img_url = "https:" + img.get('data-original')alt = img.get('alt')path = 'imgqiantuwang/' + str(alt) + '.jpg'res = requests.get(img_url)with open(path, 'wb') as f:f.write(res.content)
else:print(response.status_code)
http://www.lryc.cn/news/221010.html

相关文章:

  • 亚马逊云科技大语言模型的创新科技
  • Qt 各种数据类型
  • 电动车展示预约小程序的作用如何
  • 「随笔」浅谈2023年云计算的发展趋势
  • 高性能三防工业平板电脑 防摔防爆电容屏工控平板
  • mac flutter pb解析报错:protoc-gen-dart: program not found or is not executable
  • PostgreSQL 连接是否要通过SSL,为什么使用SSL 连接后,业务部门会投诉我?
  • Linux驱动开发——USB设备驱动
  • 微服务使用指南
  • MYSQL运维篇(已完结)
  • MapReduce性能优化之小文件问题和数据倾斜问题解决方案
  • 面向萌新的数学建模入门指南
  • 基于 golang 从零到一实现时间轮算法 (二)
  • 【系统架构设计】架构核心知识: 5 系统安全性与保密性设计
  • 无人零售奶柜:革新牛奶购买体验
  • 【Mybatis小白从0到90%精讲】15: Mybatis配置打印SQL日志
  • vue3-video-play视频播放组件
  • vue项目中页面遇到404报错
  • 快手直播弹幕websocket protobuf序列化与反序列化
  • viple入门(三)
  • Vue渲染函数渲染html
  • Odoo|“视图”和“模型”之间的数据传输
  • Electron进程通信的另一种方式
  • 二次型的相关理解
  • Spring框架中用于注入构造函数参数的标签constructor-arg
  • spdlog简单介绍和使用
  • 分类模型的Top 1和Top 5
  • LinkdeList集合
  • KaiOS APN配置文件apn.json调试验证方法(无需项目全编)
  • 【qemu逃逸】HWS2017-FastCP