数据抓取常用工具

爬虫的用途:

  • 数据分析/人工智能数据集
  • 作为社交软件冷启动
  • 舆情监控
  • 竞争对手监控

写爬虫的步骤:

  1. 数据抓取
  • 库: requests、urllib、pycurl
  • 工具:curl、wget、httpie
  1. 数据分析
  2. 数据存储

常用工具的使用:

1. curl:

安装:
apt install curl

安装的时候可能会报错,有可能是openssl没装

apt install openssl
apt install openssl-dev
使用:
curl www.baidu.com

终端返回服务端返回的数据,所以可以认为curl是一个终端上的浏览器,只不过不会对请求的数进行解析、渲染。

参数:
参数说明示例
-A设置user-agentcurl -A “Chrome” http://www.baidu.com
-X用指定方法请求curl -x POST http://www.httpbin.org/post
-I只返回请求的头信息curl -I http://www.baidu.com
-d以POST方法请求url,并发送相应的参数-d a=1 -d b=2
-d “a=1&b=2”
-d @ filename
-O下载文件并以远程的文件名保存curl -O http://www.httpbin.org/image/jpeg
-o下载文件并以指定的文件名保存curl -o fox.jpeg http://www.httpbin.org/image/jpeg
-L跟随重定向curl -IL http://www.baidu.com
-H设置头信息curl -o image.png -H “accept:image/png” http://www.httpbin.org/image
-k允许发起不安全的SSL请求curl -k https://www.12306.cn
-b设置cookiescurl -b a=test http://www.httpbin.org/cookies
-v显示连接过程中的所有信息

2. wget:

安装:
apt install wget

参数:

参数说明示例
-O以指定文件名保存下载的文件wget -O test.png http://www.httpbin.org/image/png
–limit-rate以指定的速度下载目标文件–limit-rate=200k
-c断点续传
-b后台下载
-U设置User-Agent
–mirror镜像某个目标网站
-p下载页面中的所有相关资源

例:镜像下载整个网站保存到本地,并将链接的相对路径改为绝对路径

wget -c --mirror -U "Mozilla" -p --convert-links http://doc.python-requests.org

3. httpie

功能更加强大:

  • 直观的语法
  • 格式化和色彩化的终端输出
  • 内置 JSON 支持
  • 支持上传表单和文件
  • HTTPS、代理和认证
  • 任意请求数据
  • 自定义头部
  • 持久性会话
  • 类 Wget 下载
  • 支持 Python 2.6, 2.7 和 3.x
  • 支持 Linux, Mac OS X 和 Windows
  • 插件
  • 文档
  • 测试覆盖率
安装
apt install httpie
基本操作
模拟提交表单
http -f POST yhz.me username=nate
 
显示详细的请求
http -v yhz.me
 
只显示Header
http -h yhz.me
 
只显示Body
http -b yhz.me
 
下载文件
http -d yhz.me
 
请求删除的方法
http DELETE yhz.me
 
传递JSON数据请求(默认就是JSON数据请求)
http PUT yhz.me name=nate password=nate_password
如果JSON数据存在不是字符串则用:=分隔,例如
http PUT yhz.me name=nate password=nate_password age:=28 a:=true streets:='["a", "b"]'
 
模拟Form的Post请求, Content-Type: application/x-www-form-urlencoded; charset=utf-8
http --form POST yhz.me name='nate'
模拟Form的上传, Content-Type: multipart/form-data
http -f POST example.com/jobs name='John Smith' file@~/test.pdf
 
修改请求头, 使用:分隔
http yhz.me  User-Agent:Yhz/1.0  'Cookie:a=b;b=c'  Referer:http://yhz.me/
 
认证
http -a username:password yhz.me
http --auth-type=digest -a username:password yhz.me
 
使用http代理
http --proxy=http:http://192.168.1.100:8060 yhz.me
http --proxy=http:http://user:pass@192.168.1.100:8060 yhz.me
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python数据抓取是指使用Python编程语言来获取网页上的数据。根据提供的引用内容,可以使用不同的方法来实现数据抓取。 一种常用的方法是使用正则表达式。正则表达式是一种用于文字匹配的工具,可以根据特定的模式来搜索和提取网页中的数据。通过使用re模块,可以编写正则表达式来匹配网页中的特定内容,并将其提取出来。例如,可以使用re.findall函数来获取网页中的国家名称和概况。 另一种方法是使用BeautifulSoup库。BeautifulSoup是一个用于网页解析的库,可以将HTML代码拆分成易于处理的结构,并提供了一些方便的方法来获取和操作数据。通过使用BeautifulSoup库,可以直接从网页中提取所需的数据,而无需编写复杂的正则表达式。 还有一些其他的方法,如使用urllib库来指定URL并进行网页爬取,使用xlwt库进行Excel操作,使用sqlite3库进行SQLite数据库操作等。 综上所述,Python数据抓取可以通过使用正则表达式、BeautifulSoup库以及其他相关库来实现。具体的实现方法可以根据具体的需求和情况选择合适的方法。 #### 引用[.reference_title] - *1* *2* [Python 学习 02 —— Python如何爬取数据](https://blog.csdn.net/qq_39763246/article/details/118584819)[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^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [python 数据抓取三种方法](https://blog.csdn.net/heheyangxyy/article/details/113730886)[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^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值