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

python--常用简单功能

os函数获取上层目录

# 获取当前目录
print(os.path.abspath(os.path.dirname(__file__)))
# 获取上级目录
print(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
print(os.path.abspath(os.path.dirname(os.getcwd())))
print(os.path.abspath(os.path.join(os.getcwd(), "..")))
# 获取上上级目录
print(os.path.abspath(os.path.join(os.getcwd(), "../..")))

中文转为 url 编码

from urllib.parse import quote
print(quote("xxxx"))

二次 xpath 提取

<div id="main">
      <div  id="main1">
               <ul>
                   <li>1</li>
                   <li>2</li>
                   <li>3</li>
                   <li>4</li>
           </ul>
       </div>
   </div>
div = html.xpath("//div[@id='main']")
li = div.xpath("div[@id='main1']/li")

scrapy在爬虫文件中导入 items 文件中的类

"""items.py"""
class FirstItem(scrapy.Item):
    shopName = scrapy.Field()
    start = scrapy.Field()
    commentNumber = scrapy.Field()
    avgPrice = scrapy.Field()
    shopType = scrapy.Field()
    shopAddress = scrapy.Field()
    isGroupBuy = scrapy.Field()
    groupBuyContent = scrapy.Field()
"""spider.py"""
from ..items import FirstItem

创建 Scrapy 项目

创建项目的命令:
 
scrapy startproject MySpider
 
cd MySpider
 
在已有 scrapy 项目下创建爬虫:
scrapy genspider example example.com


http://www.kler.cn/a/272456.html

相关文章:

  • 阿里云下载安装centos
  • 【02】JavaScript基础
  • 【JAVA笔记】IDEA配置本地Maven
  • JAVA代理模式梳理
  • 如何在 iPhone 上使用蓝牙鼠标
  • 【系统性】 循序渐进学C++
  • 【tls招新web部分题解】
  • 惠海 H6213E 降压恒压芯片 12V24V36V48V60V72V100V150V 转12V9V5V3.3V 车充/仪表供电方案IC
  • Linux环境下用IDEA运行Golang记录
  • SAR ADC教程系列5——FFT频谱泄露以及相干采样
  • el-input设置max、min无效的解决方案
  • VSCODE的常用插件
  • QTextToSpeech的使用——Qt
  • python中pyinstaller打包带资源的程序-pgzreo
  • 修复cython使用的bug,在mac上实现了编译,整理了cython和numba等加速文件,提供了一键编译
  • 固态硬盘有缓存和没缓存有什么区别
  • 《数据结构》复试问答题总结
  • Flutter第五弹:Flutter布局
  • 实验11-2-5 链表拼接(PTA)
  • 「Linux系列」Linux 文件与目录管理