python爬虫用到的工具和类库

本文涉及的产品
云数据库 Tair(兼容Redis),内存型 2GB
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
简介: python爬虫用到的工具和类库

开发工具

python https://www.python.org/

pycharm https://www.jetbrains.com/pycharm/

可以直接去官网下载安装


内置基本库

urllib re
>>> from urllib.request import urlopen
>>> response = urlopen("http://www.baidu.com")
>>> response
<http.client.HTTPResponse object at 0x1032edb38>

网络请求库

requests http://cn.python-requests.org/zh_CN/latest/


>>> import requests
>>> response = requests.get("http://www.baidu.com")
>>> response
<Response [200]>

浏览器工具

selenium https://www.seleniumhq.org/


chromedriver

google官网:https://sites.google.com/a/chromium.org/chromedriver/downloads

淘宝镜像:https://npm.taobao.org/mirrors/chromedriver/


>>> from selenium import webdriver
>>> driver = webdriver.Chrome()
>>> driver.get("http://www.baidu.com")
>>> driver.get("https://www.python.org")
>>> html = driver.page_source

phantomjs http://phantomjs.org/


>>> from selenium import webdriver
>>> dirver = webdriver.PhantomJS()
>>> dirver.get("http://www.baidu.com")
>>> html = driver.page_source

网页解析库

lxml http://lxml.de/

beautifulsoup4 https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/


>>> from bs4 import BeautifulSoup as BS
>>> html = "<html><h1></h1></html>"
>>> soup = BS(html, "lxml")
>>> soup.h1
<h1></h1>

pyquery https://pythonhosted.org/pyquery/


>>> from pyquery import PyQuery as pq
>>> html = "<html><h1>title</h1></html>"
>>> doc = pq(html)
>>> doc("html").text()
'title'
>>> doc("h1").text()
'title'

数据库

mysql https://dev.mysql.com/downloads/mysql/

redis https://redis.io/

mongobd https://www.mongodb.com/

mac os 可以使用 brew 安装 https://docs.brew.sh/


数据库包:

pymysql


>>> import pymysql  https://pypi.org/project/PyMySQL/


>>> conn = pymysql.connect(host="localhost", 
    user="root", password="123456", 
    port=3306, db="demo")
>>> cursor = conn.cursor()
>>> sql = "select * from mytable"
>>> cursor.execute(sql)
3
>>> cursor.fetchone()
(1, datetime.date(2018, 4, 14))
>>> cursor.close()
>>> conn.close()

pymongo http://api.mongodb.com/python/current/index.html


>>> import pymongo
>>> client = pymongo.MongoClient("localhost")
>>> db = client["newtestdb"]
>>> db["table"].insert({"name": "Tom"})
ObjectId('5adcb250d7696c839a251658')
>>> db["table"].find_one({"name": "Tom"})
{'_id': ObjectId('5adcb250d7696c839a251658'), 'name': 'Tom'}

redis


>>> import redis
>>> r = redis.Redis("localhost", 6379)
>>> r.set("name", "Tom")
True
>>> r.get("name")
b'Tom'

web框架包:

flask http://docs.jinkan.org/docs/flask/

django https://www.djangoproject.com/

jupyter http://jupyter.org/

运行:jupyter notebook

快捷键 增加一行:b


一条命令安装以上所有库

pip install requests selenium beautifulsoup4 pyquery pymysql pymongo redis flask django jupyter
相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
相关文章
|
2月前
|
数据采集 Web App开发 数据可视化
Python爬虫分析B站番剧播放量趋势:从数据采集到可视化分析
Python爬虫分析B站番剧播放量趋势:从数据采集到可视化分析b
|
1月前
|
数据采集 数据挖掘 测试技术
Go与Python爬虫实战对比:从开发效率到性能瓶颈的深度解析
本文对比了Python与Go在爬虫开发中的特点。Python凭借Scrapy等框架在开发效率和易用性上占优,适合快速开发与中小型项目;而Go凭借高并发和高性能优势,适用于大规模、长期运行的爬虫服务。文章通过代码示例和性能测试,分析了两者在并发能力、错误处理、部署维护等方面的差异,并探讨了未来融合发展的趋势。
115 0
|
22天前
|
程序员 测试技术 开发者
Python装饰器:简化代码的强大工具
Python装饰器:简化代码的强大工具
147 92
|
2月前
|
数据采集 存储 C++
Python异步爬虫(aiohttp)加速微信公众号图片下载
Python异步爬虫(aiohttp)加速微信公众号图片下载
|
25天前
|
人工智能 自然语言处理 安全
Python构建MCP服务器:从工具封装到AI集成的全流程实践
MCP协议为AI提供标准化工具调用接口,助力模型高效操作现实世界。
291 1
|
26天前
|
数据采集 存储 JSON
地区电影市场分析:用Python爬虫抓取猫眼/灯塔专业版各地区票房
地区电影市场分析:用Python爬虫抓取猫眼/灯塔专业版各地区票房
|
1月前
|
数据采集 存储 Web App开发
Python爬虫库性能与选型实战指南:从需求到落地的全链路解析
本文深入解析Python爬虫库的性能与选型策略,涵盖需求分析、技术评估与实战案例,助你构建高效稳定的数据采集系统。
218 0
|
24天前
|
数据采集 监控 调度
应对频率限制:设计智能延迟的微信读书Python爬虫
应对频率限制:设计智能延迟的微信读书Python爬虫
|
27天前
|
数据采集 机器学习/深度学习 数据可视化
Python量化交易:结合爬虫与TA-Lib技术指标分析
Python量化交易:结合爬虫与TA-Lib技术指标分析
|
28天前
|
数据采集 存储 XML
Python爬虫XPath实战:电商商品ID的精准抓取策略
Python爬虫XPath实战:电商商品ID的精准抓取策略

热门文章

最新文章

推荐镜像

更多