Windwos中安装python与一些自已用的代码

windows安装Python 3.7

下载python-3.7.6.exe
然后点击程序安装 -> 勾选Add python 3.7 to PATH
然后点
Customize installation然后再记得勾选Install for all users然后安装即可
安装完后
下载地址https://bootstrap.pypa.io/get-pip.py
下载get-pip.py后,在cmd中打开

(注意python的代码用tab格式缩进)

安装爬虫工具

pip install requests

安装Chrome调用

pip install selenium
下载地址http://chromedriver.storage.googleapis.com/index.html
请下载与你电脑上的chrome对应版本的文件

#!/usr/bin/python
import re
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')#解决DevToolsActivePort文件不存在的报错
chrome_options.add_argument('window-size=800x600')#指定浏览器分辨率
chrome_options.add_argument('--disable-gpu')#谷歌文档提到需要加上这个属性来规避bug
chrome_options.add_argument('--hide-scrollbars')#隐藏滚动条, 应对一些特殊页面
chrome_options.add_argument('blink-settings=imagesEnabled=false')#不加载图片
chrome_options.add_argument('--headless')#不提供可视化页面
browser = webdriver.Chrome("C:\Program Files\Python37-32\chromedriver.exe",options=chrome_options)
browser.set_page_load_timeout(20)
def printme( url ):
	browser.get(url)
	html=browser.page_source
	list_html=re.findall(r'<div class="search-div1">(.*?)</div>',html,re.S)
	for chapter_info in list_html:
		chapterlist =re.findall(r'<a href="(.*?)">(.*?)</a>',chapter_info)[0]
		chapter_url,chapter_title=chapterlist
		chapter_url="https://m.ae86m.com%s" % chapter_url
		browser.get(chapter_url)
		chapter_html=browser.page_source
		chapter_content=re.findall(r'<div class="search-div2">(.*?)</div>',chapter_html,re.S)[1]
		chapter_title=chapter_title.replace('*','')#替换
		chapter_title=chapter_title.replace('"','`')
		chapter_title=chapter_title.replace('?','')
		fb=open('D:/test/txt/%s.txt'%chapter_title,'w',encoding='utf-8')
		fb.write(chapter_title+'\n')
		fb.write(chapter_content+'\n')
		fb.close()
		print(chapter_url,chapter_title)
		time.sleep(1)

	try:
		pageurl=re.findall(r'<a target="_self" href="([^<>]*?)" class="pagelink_a">下一页</a>',html)[0]
	except BaseException:
		print("完成所有下载")
		browser.quit()
	else:
		pageurl="https://网址.com%s" % pageurl
		print(pageurl)
		printme(pageurl)
	return
url1="https://网址.com/arttypehtml/7-71.html"
printme(url1)

#!/usr/bin/python
import os
import pymysql
db = pymysql.connect("localhost","root","123456","bookbox")
cursor = db.cursor()
path="d:/test/txt/"
dirs =os.listdir(path)
for file in dirs:
    fileurl=path+file
    fb=open(fileurl,mode="r+",encoding='UTF-8')
    next(fb)
    line=fb.read(20)
    line=line.replace('\'','`')
    line=line.replace('\"','')
    sql = "insert into booksection(section_id,section_name,section_content,section_addtime,book_id) select(MAX(section_id)+1),'%s','%s',NOW(),29FROM booksection WHERE book_id=18"%(file,line)
    cursor.execute(sql)
    data = cursor.fetchone() 
    print ("Database version : %s " % data) 

db.close()

调用C++动态库来粘贴

#!/usr/bin/python
import os
import re
import time
import pyperclip
import ctypes
from win32com.shell import shell,shellcon
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')#解决DevToolsActivePort文件不存在的报错
chrome_options.add_argument('window-size=800x600')#指定浏览器分辨率
chrome_options.add_argument('--disable-gpu')#谷歌文档提到需要加上这个属性来规避bug
chrome_options.add_argument('--hide-scrollbars')#隐藏滚动条, 应对一些特殊页面
chrome_options.add_argument('blink-settings=imagesEnabled=false')#不加载图片
#chrome_options.add_argument('--headless')#不提供可视化页面
browser = webdriver.Chrome("C:\Program Files (x86)\Python37-32\chromedriver.exe",options=chrome_options)
browser.set_page_load_timeout(20)
browser.get("http://192.168.1.192/editsection.php")
time.sleep(1)
m=browser.find_element_by_id('booksselect')
m.find_element_by_xpath('//option[@value="18"]').click()
path="d:/test/txt/"
dirs =os.listdir(path)
for file in dirs:
    fileurl=path+file
    fb=open(fileurl,mode="r+",encoding='UTF-8')
    next(fb)
    line=fb.read()
    fb.close()
    line=line.replace('\'','`')
    pyperclip.copy(line)
    file=file.replace('.','')
    file=file.replace('txt','')
    time.sleep(1)
    sectionname=browser.find_element_by_id('sectionname')
    sectionname.clear()
    sectionname.send_keys(file)
    div1=browser.find_element_by_id('div1')
    div1.clear()
    div1.send_keys("")
    pDll=ctypes.WinDLL("Win32_paste.dll")
    pResutl= pDll.paste()   
    time.sleep(1)
    browser.find_element_by_id('btn1').click()
    time.sleep(1)
    message=browser.switch_to_alert().text
    browser.switch_to_alert().accept()
    print("%s-章节:%s"%(message,file))
    shell.SHFileOperation((0,shellcon.FO_DELETE,fileurl,None, shellcon.FOF_SILENT | shellcon.FOF_ALLOWUNDO | shellcon.FOF_NOCONFIRMATION,None,None))  

C++的代码

.h
#pragma once
class Mymath {
	int paste();
};
.c
//
#include <Windows.h>
#define DLLEXPORT extern "C" __declspec(dllexport)
#include"pray.h"
//两数相加
DLLEXPORT int  paste() {
	Sleep(10);
	keybd_event(17, NULL, KEYEVENTF_EXTENDEDKEY | 0, NULL);
	keybd_event(86, NULL, KEYEVENTF_EXTENDEDKEY | 0, NULL);
	keybd_event(86, NULL, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, NULL);
	keybd_event(17, NULL, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, NULL);
	return 1;
}
posted @ 2020-06-08 10:29  天祈笨笨  阅读(49)  评论(0编辑  收藏  举报