分类目录归档:未分类

创建加载virtio驱动的Windows 10安装镜像

如今虚拟化已经非常流行,当我们使用Linux桌面环境时,可以通过安装libvirtQEMU直接使用基于内核的虚拟化(KVM)来创建虚拟机并安装其他类型的操作系统。在基于Linux的服务器上,也可以通过oVirt或者PVE等基于KVM的虚拟化方案来实现虚拟机环境。

当我们想通过官方iso系统镜像安装比较新的Windows(例如Windows 10,Windows Server 2019等),在进入到选择安装磁盘,会发现找不到创建的虚拟磁盘,如下图所示

这是因为在官方的iso镜像中的Widnows未包含针对KVM的virtio-win驱动,因此我们可以基于Windows的iso镜像,加载virtio-win的相应驱动之后,重新创建一个包含了virtio-win驱动的iso镜像文件。

关于virtio-win的更多信息,可以参考 https://www.linux-kvm.org/pag… 。

前提条件

为了创建一个加载virtio-win驱动之后的iso镜像文件,我们需要以下准备:

  1. 具有管理员权限的Windows 10工作系统并安装Windows ADK
  2. Windows 10的安装iso文件(这里以Windows 10作为例子)
  3. virtio-win驱动的iso文件 (https://fedorapeople.org/grou…
  4. UltraISO工具

准备工作

创建工作目录

假设在你的Windows 10系统上有D盘,那我们在D盘创建相应的工作目录,以管理员权限打开PowerShell,并执行

PS D:\> mkdir D:\mnt\windows_temp,D:\mnt\boot,D:\mnt\install,D:\virtio-win

提取Windows安装文件

使用UltraISO工具打开windows 10的iso文件,并将所有文件提取到目录 *D:\mnt\windows_temp* 下

然后给Windows的镜像文件授权读写

PS D:\> attrib -r C:\mnt\windows_temp\sources\*.wim /s

提取virtio驱动文件

使用UltraISO打开下载的virtio-win的iso文件,同样提取到目录 *D:\virtio-win* 下,然后查看有哪些w10(针对windowns10)的驱动

我们可以看到在 0.1.196 版本中,包含了以下w10(64位)的驱动,为了方便后面一条命令加载所有驱动,我们把这些驱动重新放到一个目录下

PS D:\> cd virtio-win\
PS D:\virtio-win\> mkdir w10
PS D:\virtio-win\> cp -r .\Balloon\w10\amd64\ .\w10\Balloon
PS D:\virtio-win\> cp -r .\NetKVM\w10\amd64\ .\w10\NetKVM
PS D:\virtio-win\> cp -r .\pvpanic\w10\amd64\ .\w10\pvpanic
PS D:\virtio-win\> cp -r .\qemufwcfg\w10\amd64\ .\w10\qemufwcfg
PS D:\virtio-win\> cp -r .\qemupciserial\w10\amd64\ .\w10\qemupciserial
PS D:\virtio-win\> cp -r .\qxldod\w10\amd64\ .\w10\qxldod
PS D:\virtio-win\> cp -r .\sriov\w10\amd64\ .\w10\sriov
PS D:\virtio-win\> cp -r .\viofs\w10\amd64\ .\w10\viofs
PS D:\virtio-win\> cp -r .\viogpudo\w10\amd64\ .\w10\viogpudo
PS D:\virtio-win\> cp -r .\vioinput\w10\amd64\ .\w10\vioinput
PS D:\virtio-win\> cp -r .\viorng\w10\amd64\ .\w10\viorng
PS D:\virtio-win\> cp -r .\vioscsi\w10\amd64\ .\w10\vioscsi
PS D:\virtio-win\> cp -r .\vioserial\w10\amd64\ .\w10\vioserial
PS D:\virtio-win\> cp -r .\viostor\w10\amd64\ .\w10\viostor

如果创建的是其他版本的Windows系统,例如Windows Server 2019,则提取对应目录(2k19/)下的驱动。

加载virtio驱动

需要加载virtio驱动到Windows镜像文件 boot.wim 和 install.wim 。

加载驱动到boot.wim

挂载提取出来的 D:\mnt\windows_temp\sources\boot.wim 文件到目录 *D:\mnt\boot* :

先获取镜像文件的索引

PS D:\> Get-WindowsImage -ImagePath D:\mnt\windows_temp\sources\boot.wim

ImageIndex       : 1
ImageName        : Microsoft Windows PE (x64)
ImageDescription : Microsoft Windows PE (x64)
ImageSize        : 1,657,563,910 bytes

ImageIndex       : 2
ImageName        : Microsoft Windows Setup (x64)
ImageDescription : Microsoft Windows Setup (x64)
ImageSize        : 1,809,575,703 bytes

然后挂载索引2

PS D:\> Mount-WindowsImage -Path D:\mnt\boot\ -ImagePath D:\mnt\windows_temp\sources\boot.wim -Index 2

Path          : D:\mnt\boot\
Online        : False
RestartNeeded : False

接下来就是加载我们之前提取出来的驱动

PS D:\> Add-WindowsDriver -Path D:\mnt\boot\ -Driver D:\virtio-win\w10\ -Recurse

可以使用下面的命令来查看驱动是否已经加载进去

PS D:\> Get-WindowsDriver -Path D:\mnt\boot\

确定加载成功后,卸载镜像挂载并保存

PS D:\> Dismount-WindowsImage -Path D:\mnt\boot\ -Save

加载驱动到install.wim

同样,首先查看 install.wim 镜像的索引(不同索引指定了同一iso文件里的版本)

PS D:\> Get-WindowsImage -ImagePath D:\mnt\windows_temp\sources\install.wim

ImageIndex       : 1
ImageName        : Windows 10 教育版
ImageDescription : Windows 10 教育版
ImageSize        : 15,543,804,395 bytes

ImageIndex       : 2
ImageName        : Windows 10 企业版
ImageDescription : Windows 10 企业版
ImageSize        : 15,543,958,390 bytes

ImageIndex       : 3
ImageName        : Windows 10 专业版
ImageDescription : Windows 10 专业版
ImageSize        : 15,540,672,790 bytes

ImageIndex       : 4
ImageName        : Windows 10 专业教育版
ImageDescription : Windows 10 专业教育版
ImageSize        : 15,543,742,813 bytes

ImageIndex       : 5
ImageName        : Windows 10 专业工作站版
ImageDescription : Windows 10 专业工作站版
ImageSize        : 15,543,773,604 bytes

挂载想要加载驱动的版本,例如专业版

PS D:\> Mount-WindowsImage -Path D:\mnt\install\ -ImagePath D:\mnt\windows_temp\sources\install.wim -Index 3

Path          : D:\mnt\install\
Online        : False
RestartNeededFalse

然后加载驱动

PS D:\> Add-WindowsDriver -Path D:\mnt\install\ -Driver D:\virtio-win\w10\ -Recurse

卸载保存

PS D:\> Dismount-WindowsImage -Path D:\mnt\install\ -Save

同理,可以继续挂载其他索引(版本),执行以上操作,加载驱动,然后保存。

创建新的iso镜像文件

这里我们使用Windows ADK带的 oscdimg 工具来创建ISO文件,安装完Windows ADK工具之后,进入目录 *C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\amd64\Oscdimg*

PS D:\> cd 'C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\amd64\Oscdimg'

PS C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\amd64\Oscdimg> .\oscdimg.exe -lcn_windows_10_virtio -m -u2 -bD:\mnt\windows_temp\boot\etfsboot.com D:\mnt\windows_temp\ D:\ISOFiles\cn_windows_10_virtio_x64.iso

只想如上命令后,就会在目录 *D:\ISOFiles* 目录下创建出一个名为 cn_windows_10_virtio_x64.iso 的文件。

最后尝试使用新创建的iso文件来安装虚拟机系统,在选择选择磁盘驱动器时,我们就可以看到我们创建的虚拟磁盘了

接下来就可以正常安装了。

总结

本文以Windows 10的iso文件作为示例,演示了如何将virtio-win驱动加载进去,并且创建新的iso文件,使得在基于KVM的虚拟环境也可以顺利加载虚拟机磁盘进行Windows的安装。当然某些虚拟厂家也会直接提供相应的签名驱动,在安装过程中可以通过多增加一个虚拟CD驱动器来加载相应的驱动。

免费商用音乐网站

使用时要注意歌曲版权说明,有些网站收集的有不同版权的歌曲,有些是CC0无版权,有些是得署名使用,也有些非商用的,这个一定要特别注意歌曲使用说明!!!

音频使用许可协议
CC-BY保留署名协议
CCO公众共享协议
您可以复制,修改,发行和演绎该音频,甚至可用于商业性
原作者已将音频贡献至公有领域,您可以复制,修改,发行
和演绎该音频,甚至可用于商业性目的,都无需考虑著作权
目的,但在这些过程中您必须保留原作者对该音频的署名.
的问题.
CC-BY-NC署名-禁止商业协议
ROYALTY-FREE免版税许可协议


1.MUSOPEN



2.Freepd

FREEPD
100%免费音乐-知识共享0
无归属-版权免费*



3.耳聆网


4.淘声网



5.Kurage Kosaku



6.ZAPSPLAT


7.蒼月音

耆月音
FREE BGM & SOUND PRODUCTION
SQUGETSUON
一可以免费使用的BGM秦材!”无需使用报告!”也可用于商业用途!*请参阅所需的使用条款


8.甘茶の音楽工房

超过500种免费音乐和BGM,可用于网络,视练,视扬,游双双和活动)免费免费EGM系材/音乐素


9.小森平免费音效

小森平的免费下载音效



10.dig.ccmixter


11.FMA

FMA
免费音乐存档
您的第一资源免费下载和免版税音乐.
支持创作者,奖励创意.



12.Free Stock Music

FREESTOCK MUSIC.



13.CCTRAX

ECCTRAY
免费和无痛的音乐下载
电子的配音技术房子慢节奏周围的其他视频溪流



14.ほのぼの可愛いフリーBGMなら



15.freesound



16.KENNEY游戏音效


17.POND5



18.DOVA-SYNDROME

image.png



引用:https://www.yuque.com/u25248622/orncyd/gbig5l

免费商用模型网站

100多个循环blender工程文件,还有部分模型贴图,CC0协议

2.Benianus 3D
blender格式,CC0协议

3.绑匪呢
绑匪老师yyds!!!可商用的skp格式模型

 

4.polyhaven
有贴图,HDR,模型,都是CC0协议的

5.ambientcg

6.3d-wolf

产品


7.Quaternius

 

8.kenney

 

9.creativetrio

10.blendfile

 

11.publicdomainmodels
CC0协议,整理下载到网盘里了
链接:https://pan.baidu.com/s/1lB3Gh_PXN7VNtah2vCbDGg
提取码:2333

12.chocofur
CC0协议,blender格式的模型,网盘下载整理到了3.0资产库里
链接:https://pan.baidu.com/s/1OYiFSkz3R-sd7zg7wYfloQ
提取码:2333

13.克利夫兰艺术博物馆
克利夫兰艺术博物馆开放了馆藏公共领域艺术品的数字版权

14.Open Access


16.TFMSTYLE
C4D模型


17.rickdellis
C4D模型

 

18.Brandon Funk

 

19.leszczynski
链接:https://pan.baidu.com/s/15MPMvw_128hKGCLjelj91A
提取码:2333



21.Davide Tirindelli
blender格式的模型,蛮多免费的产品,也有部分需要署名,具体看作者作品备注说明

22.Spot’s Kitchen

AC UNITS
FREE PACK


23.wirewheelsclub


24.cgtrader
很多格式模型,具体协议要看作者产品许可

25.Viz-People
模型不是很多,MAX模型,也有其他的

26.blenderkit

27.plaggy
CC0协议

28.VOiD1 Gaming

29.sweethome3d

30.Manu Järvinen

31.Will Fortanbary
C4D工程文件,需要署名

32.CGMAFIA

33.植物模型 Vol 60

34.3dicons
开源模型

35.3DModelsCC0
开源模型

公共领域3D模型
免费的3D模型库会经常更新.免费.
此站点上的所有3D资产均已获得CCO许可.这意味着您可以出于任何目的立
即免费下载它们.不需要信用和归属,但高度赞赏.
浏览
image.png



36.Samuel Etver

image.png
原文出处 : https://www.yuque.com/u25248622/orncyd/ahuecl

免费商用纹理贴图

1.ambientCG

2.cgbookcase

3.Poly Haven

4.Share Textures

5.3dtextures

6.Texture Ninja

7.publicdomaintextures

8.MaterialX

9.Free PBR Materials

image.png

10.chocofur

11.blendermada

12.Blender asset pack

13.BIS
需要下载插件,blender在线程序化纹理库
插件原地址:https://korchiy.gumroad.com/l/eQeFC?recommended_by=library
插件:链接:https://pan.baidu.com/s/1gLWKnDClDbvi3eTm7s4MNw 提取码:2333

14.blenderkit
这个基本上用搅拌机的都知道吧,有模型,贴图,HDR等

15.Julio Sillet 3D Art
CC-BY协议,可以商用但要署名。

16.duion
CC0协议

Windows Server 2008 服务器重启后卡死在Windows Update 页面问题处理

服务器

  1. 服务器是联想RD640
  2. 操作系统Windows Server 2008 R2 Enterprise版
  3. 补丁版本是SP1
  4. 远程windows服务器时,一直处于远程建立连接的状态,也不提示远程失败,一直远程不上去
  5. 直接去机房本地连接键鼠和显示屏,无反应。
  6. 重启服务器一直卡在如配置Windows Update界面,等待半小时以上无法应,或者服务器自动重启,重启后再一次进入“准备配置Windows”界面如此反复。见下图。

问题描述

  1. 也有可能是配置windows百分比格式的进度显示,但是服务器也会自动重启,重启后再一次进入“准备配置Windows”界面如此反复。
  1. 重要:即使进入安全模式,也跳不过windows update这一关,只不过显示的界面是“配置Windows Update失败 还原配置 请勿关闭计算机”,此过程也会伴有重启现象。

问题分析

         出现该问题一般是由于,服务器没有关闭“Windows Update”,建议在服务器上设置更新机制为“从不检查更新(不推荐)”。禁用windows uopdate方法很多,也可以在服务列表中禁用windows update。

处理方法和思路

  1. 重启服务器按“F8”,选择“使用最近一次正确配置启动”。
  2. 重启服务器按“F8”,选择“修复计算机”。

         尝试前两种方法,均没能解决问题,最后只能使用第三种方法——通过各种方法关闭“Windows Update”。

关闭Windows Update

  1. 使用老毛桃,或者大白菜制作windows PE ,U盘启动盘。
  2. 将U盘插入服务器USB口,重启服务器。按F2进入boot,等待约3分钟会弹出一个选择启动方式的界面,选择带“UEFI+Upan名称的”选项,由此进入Windows PE。
  3. 删除“C:\Windows\SoftwareDistribution\Download\”目录下所有文件和“C:\Windows\winsxs\pending.xml”文件。如下图。
  1. 进入C:\Windows,点击regedit.exe。在注册表中做一下配置,关闭Windows Update。

依次点击如下如图所示路径

  1. 第2步:依次展开HKEY_CURRENT_USER \Software \Microsoft \Windows \CurrentVersion \Policies \Explorer项。
  2. 第3步:在右边窗口中新建一个DWORD值,将其命名为“NoWindowsUpdate”,然后将其数值数据修改为“1”,如下图所示。
  3. 4第4步:关闭注册表编辑器,然后重新启动电脑即可。       之后update就是下图:

在右侧空白区域,新建一个DWORD至,命名为“NoWindowsUpdate”,然后双击打开,将其值设为“1”。

  1. 重启服务器,进入安全模式,此时还是会显示“配置 Windows Update失败”,稍安勿躁约等2分钟即可进入windows安全模式。
  1. 在安全模式中关闭windows Update,然后正常启动windows.

How to get started with the new Office app (preview) on Android

Microsoft Office app in App Store

Source: Windows Central

Microsoft is developing a new Office mobile app for Android and iOS devices, which is similar to the experience for Windows 10, but it’s a lot more useful. Unlike the lighter desktop experience, the new app combines all the productivity apps and tools into a single install package that takes less space on your phone. This means that with a single download, you can access Word, Excel, and PowerPoint without having to switch apps, and you even get access to other features, such as Lens, Notes, and PDFs from one place.

Amazon’s 12 Days of Deals is here with big discounts for all

However, the Office app doesn’t introduce new features. Instead, it surfaces all the tools and features already available into an user-friendly interface. For example, the app gives you quick access to your recent documents and notes, and there’s a new “Actions” pane with quick access to transfer files, extract text from images, and manage PDF files, including the ability to sign documents, and much more.

The app is in the early stages of development, but there’s a preview that anyone can download and use to test the new experience and features expected to arrive when the final version releases.

In this guide, we’ll walk you through the steps to get started with the new Office mobile app on your Android device.

How to install Office app on Android

To install the latest preview of the Office app on Android from your PC, use these steps:

  1. Open Google Groups website.
  2. Click the Sign in button in the top-right.

    Google Groups sign inSource: Windows Central

  3. Sign in using your Google account connected to your Android phone.
  4. Open the Office app preview for Android group.
  5. (Optional) Clear the Link my Google profile and show my photo on posts option.
  6. Click the Join this group button.

    Office preview Google GroupsSource: Windows Central

  7. Open the Microsoft Office app in the Google Play website. (The link will only work after joining the group.)
  8. Click the Install button.

    Microsoft Office app Google PlaySource: Windows Central

  9. Select your mobile phone from the list.
  10. Click the Install button.

    Android app remove installSource: Windows Central

  11. Click the OK button.

Once you complete the steps, the new Office app will install automatically on your phone.

How to use Office app on Android

To set up the Office app preview on your phone, use these steps:

  1. Open Office app on your phone.
  2. Tap the Done button.
  3. Tap the Connect your account option.

    Quick note: You don’t need to connect your account to use the app, but signing in will allow you to access and edit documents on your OneDrive account.

  4. Enter your Microsoft account.
  5. Click the Next button.

    Set up Office app on AndroidSource: Windows Central

  6. Confirm your account password.
  7. Click the Sign in button.
  8. Tap the Done button.

After you complete the steps, you can start using the new Office app and all its new features.

Getting started with Office app

The app is divided into three sections. The top header allows you to access your account information and settings as well as a Home menu to filter documents by format (Word, Excel, PowerPoint, PDF, Media, and Notes).

Office app top optionsSource: Windows Central

In the top-left, you can access the Browse option to look for files on different accounts and locally stored on the phone manually, and the option to search files.

In the middle section is where your recent and recommended documents are listed. You can simply tap the document to open it with the appropriate application without leaving the Office experience.

Then at the bottom, you’ll find another Home button, a Plus button to create notes, snap images using Lens, or create a new Word, Excel, and PowerPoint document.

Scanning text into Word

For instance, if you want to grab text from a printed document, brochure, street sign, etc., you can snap a picture and then let Lens do the rest using these steps:

  1. Tap the Plus button.
  2. Tap the Lens option.
  3. Snap a picture.
  4. Tap the File type option to select the format.

    Office Lens optionSource: Windows Central

  5. Tap the Done button

Alternatively, you can tap the Plus button, then the Documents option, and then select whether to scan directly to Word, Excel, or PowerPoint. Also, from the Documents menu, you can create new blank documents, workbooks, and presentations.

Then, there’s the Actions button, which allows you to access a number of common tasks, such as to send or receive files from your phone to a computer or nearby phone. Also, you’ll find the same options to scan an image or table to extract text or table information into a Word or Excel document.

Transferring files from phone to computer

If you have to transfer a file from your phone to another computer, the process is straightforward (no account or OneDrive setup required), you only need to follow these steps:

  1. On the computer receiving the file, open the browser.
  2. Open the aka.ms/TransferFiles page.

    Office file transfer pageSource: Windows Central

  3. On the phone, tap the Actions button from the Office app.
  4. Under the “Share files” section, tap the Transfer Files option.
  5. Tap the Send file button. (You can also use this feature to receive files.)
  6. Using the phone camera, scan the QR code on the browser.

    Office transfer filesSource: Windows Central

  7. Tap the Pair button on your phone.
  8. Tap the Pair button on your computer.
  9. Select the file that you want to transfer.

    Pairing phone and PC using Office appSource: Windows Central

  10. Save the file on your computer.

In addition to send documents, you can also use this feature to transfer files from your device to your phone without the need of a Microsoft account.

Another interesting feature is the ability to manage PDF documents in a number of ways. Using the Office mobile app, you can add a signature to a PDF using just your finger. You can scan a picture to a PDF file, or create a document from an existing image, as well as convert an Office file into a PDF.

Signing PDF document

For example, if you want to sign a PDF document using the Office mobile app, you can use these steps:

  1. Tap the Actions button from the Office app.
  2. Tap the Sign a PDF option.
  3. Tap the Browse button from the top-right corner.
  4. Open the document.

    Quick note: The document must have editing permissions for the signature feature to work.

  5. Tap where you to apply a signature.

    Office PDF signatureSource: Windows Central

  6. Select the ink color (black, blue, or red).
  7. Sign the document in the space provided using your finger.
  8. Tap the Finish button in the top-right.

    Add signature PDF file

  9. (Optional) tap and drag the signature to the correct place.
  10. (Optional) tap the trash can icon to delete the signature.
  11. Tap the Finish button in the top-left corner

Finally, the Actions pane includes a Scan QR Code option to scan links or text and save it as a note in the app.

Although most of these features have been available for a long time, the new Office mobile app makes them easier to find and use, and bringing Word, Excel, and PowerPoint to the central hub removes the frictions of moving back and forth between apps.

During my short time testing the app, the overall experience felt really good, and the menus were intuitive and features easy to understand.

We’re focusing this overview on Android devices, but the new experience is also available on iOS. However, at the time of this writing, the test flight program has reached its limit of participants.

Android五种数据存储方式

android的五种数据存储方式

  • 文件存储
  • SharedPreferences
  • SQLite数据库存储
  • ContentProvider
  • 网络存储
一、文件存储

默认存储路径:/data/data/<PackageName>/files
文件操作模式:MODE_PRIVATE(默认):覆盖、MODE_APPEND:追加

  • 写入文件
public void save(){
      String data = "save something here";
      FileOutputStream out = null;
      ButteredWriter writer = null;
      try{
            out = openFileOutput("data",Context.MODE_PRIVATE);
            writer = new ButteredWriter(new OutputSreamWriter(out));
            writer.write(data);
      }catch(IOException e){
            e.printStackTrace();
      }finally{
            try{
                  if(writer!=null){
                        writer.close();
                  }
            }catch(IOException e){
                  e.printStackTrace();
       }
}
  • 读取数据
public String load(){
      FileInputStream in = null;
      ButteredReader reader = null;
      StringBuilder builder = new StringBuilder();
      try{
            in = openFileInput("data");
            reader = new ButteredReader(new InputStreamReader(in));
            String line= "";
            while((line = reader.readline()) != null){
                   builder.append();
            }
      }catch(IOException e){
            e.printStackTrace();
      }finally{
            if(reader != null){
                    try{
                          reader.close();
                    }catch(IOException e){
                          e.printStackTrace();
                    }
             }
      }
}
二、SharedPreferences

默认存储路径:/data/data/<PackageName>/shared_prefs
操作模式:MODE_PRIVATE(默认):只有当前的应用程序才能对文件进行读写、MODE_MULTI_PROCESS:用于多个进程对同一个SharedPreferences进行读写。
存储数据格式:键值对

获取SharedPreferences对象的方法
  • Context的getSharedPreferences()方法,参数一是文件名,参数二是操作模式
  • Activity的getPreferences()方法,参数为操作模式,使用当前应用程序包名为文件名
  • PreferenceManager的getDefaultSharedPreferences()静态方法,接收Context参数,使用当前应用程序包名为文件名
存储数据
  • 调用SharedPreferences对象的edit()方法获取一个SharedPreferences.Editor对象
  • 向Editor对象中添加数据putBoolean、putString等
  • 调用commit()方法提交数据
SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
editor.putString("name","ZhangSan");
editor.putInt("age",12);
editor.putBoolean("isMarried",false);
editor.commit();
从SharedPreferences文件中读取数据
SharedPreferences pref = getSharedPreferences("data",MODE_PRIVATE);
String name = pref.getString("name");
int age = pref.getInt("age");
boolean isMarried = pref.getBoolean("isMarried");
三、SQLite数据库存储

默认存储路径:/data/data/<PackageName>/databases
数据类型

  • integer 整型
  • real 浮点型
  • text 文本类型
  • blob 二进制类型
public class MyDatabaseHelper extends SQLiteOpenHelper{  
    public static final String CREATE_BOOK = "create table book ( "
               + " id integer primary key autoincrement,"
               + " author text,"
               + "price real,"
               + "pages integer,"
               + "name text)"; 
    private Context context;
    public MyDatabaseHelper (Context context, String name, CursorFactory factory, int version) {  
        super(context, name, factory, version);  
        this.context = context;
    }  

    @Override  
    public void onCreate(SQLiteDatabase db) {  
        db.execSQL(CREATE_BOOK);          
    }  
      
    //当打开数据库时传入的版本号与当前的版本号不同时会调用该方法  
    @Override  
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {    
    
    }    
}  

在MainActivity中

MyDatabaseHelper helper = new MyDatabaseHelper(this,"BookStore.db",null,1);
 //检测到没有BookStore这个数据库,会创建该数据库并调用MyDatabaseHelper中的onCreated方法。
helper.getWritableDatabase(); 
升级数据库
public class MyDatabaseHelper extends SQLiteOpenHelper{  
    ......
    //当打开数据库时传入的版本号与当前的版本号不同时会调用该方法  
    @Override  
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {   
          db.execSQL("drop table if exists Book");
          onCreate(db):
    }    
} 

在MainActivity中只需将version改为大于原来版本号即可。

MyDatabaseHelper helper = new MyDatabaseHelper(this,"BookStore.db",null,2);
helper.getWritableDatabase(); 
向数据库添加数据

insert()方法,参数一表名,参数二是在未指定添加数据的情况下给某些可为空的列自动赋值为NULL,设置为null即可,参数三是ContentValues对象。
MainActivity

SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name","The Book Name");
values.put("author","chen");
values.put("pages",100);
values.put("price",200);
db.insert("Book",null,values);
更新数据库中的数据

update()方法,参数一是表名,参数二是ContentValues对象,参数三、四是去约束更新某一行或某几行的数据,不指定默认更新所有。
MainActivity

SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("price",120);
db.update("Book",values,"name= ?",new String[]{"The Book Name"});
从数据库中删除数据

delete()方法,参数一是表名,参数二、三是去约束删除某一行或某几行的数据,不指定默认删除所有。
MainActivity

SQLiteDatabase db = helper.getWritableDatabase();
db.delete("Book","pages> ?",new String[]{"100"});
查询数据库中的数据

query()方法,参数一是表名,参数二是指定查询哪几列,默认全部,参数三、四是去约束查询某一行或某几行的数据,不指定默认查询所有,参数五是用于指定需要去group by的列,参数六是对group by的数据进一步的过滤,参数七是查询结果的排序方式
MainActivity

SQLiteDatabase db = helper.getWritableDatabase();
Cursor cursor = db.query("Book",null,null,null,null,null,null);
if(cursor.moveToFirst()){
      do{
            String name = cursor.getString(cursor.getColumnIndex("name");
            String author = cursor.getString(cursor.getColumnIndex("author");
            int pages = cursor.getString(cursor.getColumnIndex("pages");
            double price = cursor.getString(cursor.getColumnIndex("price");
       }while(cursor.moveToNext());
}
cursor.close():
使用SQL语句操作数据库
//添加数据
db.execSQL("insert into Book(name,author,pages,price) values(?,?,?,?) "
            ,new String[]{"The Book Name","chen",100,20});
//更新数据
db.execSQL("update Book set price = ? where name = ?",new String[]
            {"10","The Book Name"});
//删除数据
db.execSQL("delete from Book where pages > ?",new String[]{"100"});
//查询数据
db.execSQL("select * from Book",null);
使用事务操作
SQLiteDatabase db = helper.getWritableDatabase();
db.beginTransaction();  //开启事务
try{
      ......
      db.insert("Book",null,values);
      db.setTransactionSuccessful();  //事务成功执行
}catch(SQLException e){
      e.printStackTrace();
}finally{
      db.endTransaction();  //结束事务
}
四、ContentProvider

ContentProvider主要用于不同的程序之间实现数据共享的功能。

  • 访问其他应用程序中的数据

工具类ContentResolver,提供了一系列方法对数据进行CRUD操作。

ContentResolver的使用方法

1、内容URI
内容URI是由权限和路径组成的,权限是用于区分不同的应用程序,一般是以包名来命名。路径是用于区分同一个应用程序的不同表。

//包名为com.example.app的表table1访问路径
Uri uri  = Uri.parse("content://com.example.app.provider/table1");

2、使用Uri对象进行数据操作

  • 查询
Cursor cursor = getContentResolver().query(uri,null,null,null,null);
if(cursor != null){
      while(cursor.moveToNext()){
            String column1 = cursor.getString(cursor.getColumnIndex("column1"));
            String column2 = cursor.getString(cursor.getColumnIndex("column2"));
      }
      cursor.close();
}
  • 插入
ContentValues values = new ContentValues();
values.put("column1","text");
values.put("column2",1);
getContentResolver().insert(uri,values);
五、网络存储

作者:cy_why
链接:https://www.jianshu.com/p/536ca489a7f4
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

下载百度文库的文档老要下载劵!用Python“破解”这个限制!

前言

大家都应该有过从百度文库下载东西的经历,对于下载需要下载券的文章,我们可以办理文库VIP(土豪的选择):

有的人也会在某宝购买一定的下载券,然后进行下载。而另一些勤勤恳恳的人,则会选择上传文章,慢慢攒下载券。任劳任怨的人,则会自己一点一点的复制粘贴,复制到word里文字太大,那就复制到txt文件里。而既不想花钱又不想攒下载券,也不想一点一点复制粘贴的人,会选择“冰点文库”这样的下载软件,不过貌似现在“冰点文库”已经不能使用了。当然,还有一些其他破解方法,比如放到手机的百度文库APP里,另存为文章,不需要下载券就可以下载文章。诸如此类的方法,可谓五花八门。而对于学习爬虫的人来说,面对怎样免费下载一个付费的word文章的问题,第一个想到的应该就是:自己写个程序搞下来。

2

问题分析

我们以如何下载下面这篇文章为例,分析问题:

URL : https://wenku.baidu.com/view/aa31a84bcf84b9d528ea7a2c.html

我想,看到这样的一个文章,如果爬取当前页面的内容还是很好爬的吧。感觉so easy!至少我当时是这么想的,但是当把文章翻到最下方的时候,我看到了如下内容:

呃….需要点击“继续阅读”才能显示后续的内容,我单爬这一页内容,是爬不到后续的内容的。第一个想到的方法是,抓包分析下,然后我又一次蒙逼了:

Request URL这么长!!最后的expire时间信息好解决,其他的信息呢?不想做无谓的挣扎,因此,我果断地放弃这个方法。

问题:获取当前页的内容好办,怎么获取接下来页面的内容?

带着这个思考,Selenium神器走入了我的视线。

3

预备知识

3.1 Selenium

3.1.1 简介

Selenium 是什么?一句话,自动化测试工具。它支持各种浏览器,包括 Chrome,Safari,Firefox 等主流界面式浏览器,如果你在这些浏览器里面安装一个 Selenium 的插件,那么便可以方便地实现Web界面的测试。换句话说叫 Selenium 支持这些浏览器驱动。Selenium支持多种语言开发,比如 Java,C,Ruby等等,而对于Python,当然也是支持的!

3.1.2 安装

3.1.3 基础知识

3.1.3.1 小试牛刀

我们先来一个小例子感受一下 Selenium,这里我们用 Chrome 浏览器来测试。

运行这段代码,会自动打开浏览器,然后访问百度。

如果程序执行错误,浏览器没有打开,那么应该是没有装 Chrome 浏览器或者 Chrome 驱动没有配置在环境变量里。下载驱动,然后将驱动文件路径配置在环境变量即可。

驱动下载地址:https://sites.google.com/a/chromium.org/chromedriver/downloads

windows下设置环境变量的方法:,输入,点击确定,出现如下对话框:

选择高级->环境变量。在系统变量的Path变量中,添加驱动文件路径即可(注意:分号)。

Linux的环境变量也好设置,在文件中export即可,记得。

当然,你不设置环境变量也是可以的,程序可以这样写:

上面的 是你的chrome驱动文件位置,可以使用绝对路径。我们通过驱动的位置传递参数,也可以调用驱动,结果如下图所示:

3.1.3.2 模拟提交

下面的代码实现了模拟提交提交搜索的功能,首先等页面加载完成,然后输入到搜索框文本,点击提交,然后使用page_source打印提交后的页面的信息。

全自动的哦,程序操控!是不是很酷炫?

其中 driver.get 方法会打开请求的URL,WebDriver 会等待页面完全加载完成之后才会返回,即程序会等待页面的所有内容加载完成,JS渲染完毕之后才继续往下执行。注意:如果这里用到了特别多的 Ajax 的话,程序可能不知道是否已经完全加载完毕。

WebDriver 提供了许多寻找网页元素的方法,譬如 的方法。例如一个输入框可以通过 方法寻找 name 属性来确定。

然后我们输入来文本然后模拟点击了回车,就像我们敲击键盘一样。我们可以利用 Keys 这个类来模拟键盘输入。

最后最重要的一点是可以获取网页渲染后的源代码。通过,输出 属性即可。这样,我们就可以做到网页的动态爬取了。

3.1.3.3 元素选取

关于元素的选取,有如下API:

单个元素选取:

多个元素选取:

另外还可以利用 By 类来确定哪种选择方式:

By类的一些属性如下:

这些方法跟JavaScript的一些方法有相似之处,,就是根据标签的id属性查找元素,,就是根据标签的name属性查找元素。举个简单的例子,比如我想找到下面这个元素:

我们可以这样获取它:

前三个都很好理解,最后一个xpath什么意思?这个无需着急,xpath是非常强大的元素查找方式,使用这种方法几乎可以定位到页面上的任意元素,在后面我会进行单独讲解。

3.1.3.4 界面交互

通过元素选取,我们能够找到元素的位置,我们可以根据这个元素的位置进行相应的事件操作,例如输入文本框内容、鼠标单击、填充表单、元素拖拽等等。由于篇幅原因,我就不一一讲解了,主要讲解本次实战用到的鼠标单击,更详细的内容,可以查看官方文档。

比如上面这句话,我使用找到元素位置,暂且不用理会这句话什么意思,暂且理解为找到了一个按键的位置。然后我们使用click()方法,就可以触发鼠标左键单击事件。是不是很简单?但是有一点需要注意,就是在点击的时候,元素不能有遮挡。什么意思?就是说我在点击这个按键之前,窗口最好移动到那里,因为如果这个按键被其他元素遮挡,click()就触发异常。因此稳妥起见,在触发鼠标左键单击事件之前,滑动窗口,移动到按键上方的一个元素位置:

上面的代码,就是将窗口滑动到page这个位置,在这个位置,我们能够看到我们需要点击的按键。

3.1.3.5 添加User-Agent

使用webdriver,是可以更改User-Agent的,代码如下:

使用Android的User-Agent打开浏览器,画风是这样的(第二条新闻的图片略劲爆):

Selenium就先介绍这么多,对于本次实战内容,已经足够。那么接下来,让我们聊聊xpath。

3.2 Xpath

这个方法是非常强大的元素查找方式,使用这种方法几乎可以定位到页面上的任意元素。在正式开始使用XPath进行定位前,我们先了解下什么是XPath。XPath是XML Path的简称,由于HTML文档本身就是一个标准的XML页面,所以我们可以使用XPath的语法来定位页面元素。

假设我们现在以图所示HTML代码为例,要引用对应的对象,XPath语法如下:

绝对路径写法(只有一种),写法如下:

引用页面上的form元素(即源码中的第3行):

注意:

元素的xpath绝对路径可通过firebug直接查询。

一般不推荐使用绝对路径的写法,因为一旦页面结构发生变化,该路径也随之失效,必须重新写。

绝对路径以单/号表示,而下面要讲的相对路径则以表示,这个区别非常重要。另外需要多说一句的是,当xpath的路径以开头时,表示让Xpath解析引擎从文档的根节点开始解析。当xpath路径以开头时,则表示让xpath引擎从文档的任意符合的元素节点开始进行解析。而当出现在xpath路径中时,则表示寻找父节点的直接子节点,当出现在xpath路径中时,表示寻找父节点下任意符合条件的子节点,不管嵌套了多少层级(这些下面都有例子,大家可以参照来试验)。弄清这个原则,就可以理解其实xpath的路径可以绝对路径和相对路径混合在一起来进行表示,想怎么玩就怎么玩。

下面是相对路径的引用写法:

查找页面根元素:

查找页面上所有的input元素:

查找页面上第一个form元素内的直接子input元素(即只包括form元素的下一级input元素,使用绝对路径表示,单/号):

查找页面上第一个form元素内的所有子input元素(只要在form元素内的input都算,不管还嵌套了多少个其他标签,使用相对路径表示,双//号):

查找页面上第一个form元素:

查找页面上id为loginForm的form元素:

查找页面上具有name属性为username的input元素:

查找页面上id为loginForm的form元素下的第一个input元素:

查找页面具有name属性为contiune并且type属性为button的input元素:

查找页面上id为loginForm的form元素下第4个input元素:

Xpath功能很强大,所以也可以写得更加复杂一些,如下面图所示的HTML源码。

如果我们现在要引用id为“J_password”的input元素,该怎么写呢?我们可以像下面这样写:

也可以写成:

这里解释一下,其中//*[@id=’ J_login_form’]这一段是指在根元素下查找任意id为J_login_form的元素,此时相当于引用到了form元素。后面的路径必须按照源码的层级依次往下写。按照图(3)所示代码中,我们要找的input元素包含在一个dt标签内,而dt又包含在dl标签内,所以中间必须写上dl和dt两层,才到input这层。当然我们也可以用*号省略具体的标签名称,但元素的层级关系必须体现出来,比如我们不能写成//[@id=’J_login_form’]/input[@id=’J_password’],这样肯定会报错的。

前面讲的都是xpath中基于准确元素属性的定位,其实xpath作为定位神器也可以用于模糊匹配。本次实战,可以进行准确元素定位,因此就不讲模糊匹配了。如果有兴趣,可以自行了解。

4

动手实战

以上面提到的文章为例,进行爬取讲解。URL :https://wenku.baidu.com/view/aa31a84bcf84b9d528ea7a2c.html

4.1 页面切换

由于网页的百度文库页面复杂,可能抓取内容不全,因此使用User-Agent,模拟手机登录,然后打印文章标题,文章页数,并进行翻页。先看下这个网站。

我们需要找到两个元素的位置,一个是页码元素的位置,我们根据这个元素的位置,将浏览器的滑动窗口移动到这个位置,这样就可以避免click()下一页元素的时候,有元素遮挡。然后找到下一页元素的位置,然后根据下一页元素的位置,触发鼠标左键单击事件。

我们审查元素看一下,这两个元素:

我们根据这两个元素,就可以通过xpath查找元素位置,代码分别如下:

由于page元素有很多,所以我们使用find_elements_by_xpath()方法查找,然后使用page[-1],也就是链表中的最后一个元素的信息进行浏览器窗口滑动,代码如下:

运行效果,自动翻页了有木有!

4.2 内容爬取

爬取内容这里,使用之前重点讲过的BeautifulSoup就可以。这里不再细奖,审查元素,自己分析下就有了。代码如下:

爬取结果如下:

爬取的内容还是蛮规整的,对吧?

4.3 整体代码

我们能够翻页,也能够爬取当前页面内容,代码稍作整合,就可以爬取所有页面的内容了!找下网页的规律就会发现,5页文章放在一个网页里。思路:爬取正文内容,再根据爬取到的文章页数,计算页数/5.0,得到一个分数,如果这个分数大于1,则翻页继续爬,如果小于或等于1,代表到最后一页了。停止翻页。有一点注意一下,翻页之后,等待延时一下,等待页面加载之后在爬取内容,这里,我们使用最简单的办法,用sleep()进行延时。因此总体代码如下:

瞧,最后一页的内容也爬取下来了,接下来的工作就简单了,把这个结果写到txt文件中,我这里就不再进行讲解了。

至此,整篇的内容,我们都爬取下来了。是不是很酷?那就开始动手实践吧!

5

总结

这样爬取是可以爬取到内容,但是缺点也很明显:

没有处理图片内容,可以后续完善;

代码通用性不强,有的文章结构不是这样,需要对代码进行略微修改,才能爬取到内容;

对于上百页的内容爬取有些问题,翻页方式变了,需要换种方法处理,有兴趣的可以自己看下;

等待页面切换方法太out,可以使用显示等待的方式,等待页面加载;

selenium虽好,但是有些耗时,可以使用PhantomJS对这部分代码进行替换;

最后,我感觉我的方法可能有些low,如果有更好的方法,欢迎交流。

Win10一个命令让你掌握笔记本/平板电池健康状况

使用笔记本以及平板设备的朋友应该对设备电池健康状况比较关心,Win10系统中就提供了可查看设备电池详细使用报告的方法,感兴趣的朋友可以尝试一下。其实这项功能在Win7中就已存在,在Win8中进一步完善,报告也更加详细。

• 以管理员身份运行CMD,输入下面的命令回车,系统就会在你指定的路径生成设备电池使用报告。

powercfg /batteryreport /output “C:\battery_report.html”

• 双击C:\battery_report.html会用你默认的浏览器打开该报告,一起来简单了解一下:

▲系统以及电池基本信息,包括电池设计容量、完全充电容量、充电周期

▲设备电池完全充电容量变化

▲最近三日电池使用情况记录,包括何时处于活动状态以及何时进入待机状态

▲最近三日电池使用变化曲线

▲电池续航预估值变化,这里的数据比你在任务栏电池图标上看到的要准确

这份报告会详细记录自系统安装后电池的使用详情,根据这些数据可掌握电池续航变化情况。