Commit 08f82566a71d7fe8d0f806775106fcbef5dbd22a

Authored by 季培红
0 parents

init

Exception.py 0 → 100644
  1 +++ a/Exception.py
  1 +
  2 +try:
  3 + open("a.txt")
  4 +except FileNotFoundError as msg:
  5 + print(msg)
  6 +finally:
  7 + print("The end")
0 8 \ No newline at end of file
... ...
File_IO/File_IO.py 0 → 100644
  1 +++ a/File_IO/File_IO.py
  1 +#!/usr/bin/python
  2 +# -*- coding: UTF-8 -*-
  3 +import os
  4 +import csv
  5 +#写入
  6 +
  7 +# fo = open("foo.txt","wb")
  8 +# str = "www.room.com!\r\nVery good site!\r\n"
  9 +# fo.write(str.encode())
  10 +# fo.close()
  11 +#
  12 +# #读文件
  13 +# fo = open("foo.txt","r+")
  14 +# str = fo.read()
  15 +# print(str)
  16 +#
  17 +# #重命名
  18 +# os.rename("foo.txt","stu.txt")
  19 +
  20 +
  21 +# file = open("stu.txt","r")
  22 +# lines = file.readlines()
  23 +# print(lines)
  24 +#
  25 +# for line in lines:
  26 +# name = line.split(",")[0]
  27 +# age = line.split(",")[1]
  28 +# address = line.split(",")[2]
  29 +# print(name,age,address)
  30 +
  31 +# csv_file = csv.reader(open("stu_info.csv","r"))
  32 +# print(csv_file)
  33 +#
  34 +# for stu in csv_file:
  35 +# print(stu[1])
  36 +
  37 +stu1 = ['Marry',21,'lijiang']
  38 +stu2 = ['Job',22,'haian']
  39 +
  40 +# 打开文件
  41 +out = open('stu_info.csv','a',newline='')
  42 +# 设定写入模式
  43 +csv_write = csv.writer(out,dialect='excel')
  44 +# 写入具体内容
  45 +csv_write.writerow(stu1)
  46 +csv_write.writerow(stu2)
  47 +print("Write is over")
0 48 \ No newline at end of file
... ...
File_IO/stu.txt 0 → 100644
  1 +++ a/File_IO/stu.txt
  1 +Jack,25,Beijing
  2 +Tony,24,Shanghai
  3 +Henrry,26,shenzhen
... ...
File_IO/stu_info.csv 0 → 100644
  1 +++ a/File_IO/stu_info.csv
  1 +Jack,25,Beijing
  2 +Tony,24,Shanghai
  3 +Katherine,26,Shenzhen
  4 +Marry,21,lijiang
  5 +Job,22,haian
... ...
Guess_num.py 0 → 100644
  1 +++ a/Guess_num.py
  1 +import random
  2 +
  3 +answer = random.randint(1,100)
  4 +
  5 +n = int(input("Please input a number(1-100):"))
  6 +
  7 +while n!=answer:
  8 + if n>answer:
  9 + n = int(input("More:"))
  10 + elif n<answer:
  11 + n = int(input("Less:"))
  12 +print("You win")
  13 +
  14 +
... ...
Spider_photo.py 0 → 100644
  1 +++ a/Spider_photo.py
  1 +import urllib
  2 +import urllib.request
  3 +import requests
  4 +import re #正则表达式
  5 +
  6 +# 解析页面
  7 +def load_page(url):
  8 + request = urllib.request.Request(url) # 发送网络请求
  9 + response = urllib.request.urlopen(request) # 根据url打开页面
  10 + data = response.read() # 获取页面响应数据
  11 + return data
  12 +
  13 +
  14 +# 下载图片
  15 +def get_image(html):
  16 + regx = r'http://[\S]*jpg' #定义正则表达式,匹配页面图片
  17 + pattern = re.compile(regx) #编译表达式构造匹配模式
  18 + get_image = re.findall(pattern,repr(html)) #进行正则匹配并返回结果
  19 +
  20 + num = 1
  21 + # 遍历获取的图片
  22 + for i in get_image:
  23 + image = load_page(i)
  24 + # 将图片存入到指定文件夹
  25 + with open('E:\\spiderphoto\\%s.jpg' %num,'wb') as fb:
  26 + fb.write(image)
  27 + print("正在下载第%s张图片" %num)
  28 + num = num + 1
  29 + print("下载完成!")
  30 +
  31 +if __name__ == '__main__':
  32 + url = 'http://p.weather.com.cn/2017/06/2720826.shtml#p=1'
  33 + html = load_page(url)
  34 + get_image(html)
  35 +
  36 +
  37 +
  38 +
  39 +
  40 +
... ...
Thread_Process/Multi_process.py 0 → 100644
  1 +++ a/Thread_Process/Multi_process.py
  1 +from time import ctime,sleep
  2 +import multiprocessing
  3 +
  4 +# 定义说和写的方法
  5 +def talk(content,loop):
  6 + for i in range(loop):
  7 + print("Start talk:%s %s" %(content,ctime()))
  8 + sleep(2)
  9 +
  10 +def write(content, loop):
  11 + for i in range(loop):
  12 + print("Start write:%s %s" % (content, ctime()))
  13 + sleep(3)
  14 +
  15 +# 定义和加载说和写的进程
  16 +process = []
  17 +
  18 +p1 = multiprocessing.Process(target=talk,args=('Hello world',2))
  19 +process.append(p1)
  20 +
  21 +p2 = multiprocessing.Process(target=write,args=('Life is short You need python',2))
  22 +process.append(p2)
  23 +
  24 +# 运行多进程
  25 +if __name__ == '__main__':
  26 + for p in process:
  27 + p.start()
  28 + for p in process:
  29 + p.join()
  30 + print("All processes are end %s" %ctime())
... ...
Thread_Process/Multi_threading.py 0 → 100644
  1 +++ a/Thread_Process/Multi_threading.py
  1 +from time import ctime,sleep
  2 +import threading
  3 +
  4 +# 定义说和写的方法
  5 +def talk(content,loop):
  6 + for i in range(loop):
  7 + print("Start talk:%s %s" %(content,ctime()))
  8 + sleep(2)
  9 +
  10 +def write(content, loop):
  11 + for i in range(loop):
  12 + print("Start write:%s %s" % (content, ctime()))
  13 + sleep(3)
  14 +
  15 +# 定义和加载说和写的线程
  16 +threads = []
  17 +
  18 +t1 = threading.Thread(target=talk,args=('Hello Wold',2))
  19 +threads.append(t1)
  20 +
  21 +t2 = threading.Thread(target=write,args=('Life is short You need python',2))
  22 +threads.append(t2)
  23 +
  24 +# 运行多线程
  25 +if __name__ == '__main__':
  26 + for t in threads:
  27 + t.start()
  28 + for t in threads:
  29 + t.join()
  30 + print("All threading is end! %s" %ctime())
  31 +
... ...
Thread_Process/Single_thread.py 0 → 100644
  1 +++ a/Thread_Process/Single_thread.py
  1 +from time import ctime,sleep
  2 +
  3 +def talk():
  4 + print("Start talk:%r"%ctime())
  5 + sleep(2)
  6 +
  7 +def write():
  8 + print("Start write:%r"%ctime())
  9 + sleep(3)
  10 +
  11 +if __name__ == '__main__':
  12 + talk()
  13 + write()
  14 + print("All end!%r"%ctime())
... ...
Webdriver/Find_Elements.py 0 → 100644
  1 +++ a/Webdriver/Find_Elements.py
  1 +from selenium import webdriver
  2 +from selenium.webdriver.support.ui import WebDriverWait
  3 +from selenium.webdriver.support import expected_conditions as EC
  4 +from selenium.common.exceptions import NoSuchElementException
  5 +from selenium.webdriver.common.by import By
  6 +from time import sleep,ctime
  7 +
  8 +
  9 +driver = webdriver.Firefox()
  10 +driver.maximize_window()
  11 +
  12 +driver.get("https://dengta.vchangyi.com")
  13 +
  14 +# 登录
  15 +# driver.find_element_by_xpath("//input[@name='mobile']").send_keys('17321009308')
  16 +# driver.find_element_by_xpath("//input[@name='password']").send_keys('123456')
  17 +
  18 +# 显式等待
  19 +mobile = WebDriverWait(driver,10,0.5).until(EC.presence_of_element_located((By.NAME,"mobile")))
  20 +mobile.send_keys('17321009308')
  21 +
  22 +# 隐式等待
  23 +driver.implicitly_wait(5)
  24 +try:
  25 + print(ctime())
  26 + driver.find_element_by_name("password").send_keys('123456')
  27 + driver.find_element_by_xpath("//button").click()
  28 + # 员工管理
  29 + driver.find_element_by_link_text("员工管理").click()
  30 +
  31 + # 岗位管理
  32 + driver.find_element_by_link_text("岗位管理").click()
  33 + # 操作滚动条,滚动条距离顶部10000个像素点
  34 + driver.execute_script("window.scrollBy(0,10000)")
  35 + sleep(2)
  36 +
  37 + # 点击保存按钮
  38 + #driver.find_element_by_xpath("//button").click()
  39 +
  40 + # alert = driver.switch_to_alert()
  41 + # alert.accept()
  42 +except NoSuchElementException as msg:
  43 + print(msg)
  44 +finally:
  45 + print(ctime())
  46 +
  47 +
  48 +
  49 +
  50 +
  51 +
  52 +
... ...
Webdriver/QQ_Zone.py 0 → 100644
  1 +++ a/Webdriver/QQ_Zone.py
  1 +from selenium import webdriver
  2 +from selenium.webdriver.support.ui import WebDriverWait
  3 +from selenium.webdriver.support import expected_conditions as EC
  4 +from selenium.common.exceptions import NoSuchElementException
  5 +from selenium.webdriver.common.by import By
  6 +from time import sleep
  7 +import win32gui
  8 +import win32con
  9 +
  10 +
  11 +driver = webdriver.Firefox()
  12 +driver.maximize_window()
  13 +
  14 +driver.get("https://qzone.qq.com/")
  15 +
  16 +# 切换到frame
  17 +driver.switch_to.frame("login_frame")
  18 +
  19 +try:
  20 + # 隐式等待
  21 + driver.implicitly_wait(5)
  22 + # 切换到账号密码登录
  23 + driver.find_element_by_id("switcher_plogin").click()
  24 + # 登录
  25 + driver.find_element_by_id("u").send_keys("774355873")
  26 + driver.find_element_by_name("p").send_keys("newlife19110228.")
  27 + driver.find_element_by_id("login_button").click()
  28 +
  29 + # 退出frame,返回主文档
  30 + driver.switch_to.default_content()
  31 +except NoSuchElementException as msg:
  32 + print(msg)
  33 +
  34 +# 图片上传,显式等待
  35 +WebDriverWait(driver,10,0.5).until(EC.presence_of_element_located((By.CLASS_NAME,"pic"))).click()
  36 +driver.find_element_by_id("qz_app_imageReader_1").click()
  37 +sleep(5)
  38 +dialog = win32gui.FindWindow(0, u'打开') # 对话框
  39 +ComboBoxEx32 = win32gui.FindWindowEx(dialog, 0, 'ComboBoxEx32', None)
  40 +ComboBox = win32gui.FindWindowEx(ComboBoxEx32, 0, 'ComboBox', None)
  41 +Edit = win32gui.FindWindowEx(ComboBox, 0, 'Edit', None) # 输入相对地址
  42 +button = win32gui.FindWindowEx(dialog, 0, 'Button', None) # 确定按钮
  43 +win32gui.SendMessage(Edit, win32con.WM_SETTEXT, None, r"E:\Python+Selenium\sc4\cat.jpg")
  44 +win32gui.SendMessage(dialog, win32con.WM_COMMAND, 1, button)
  45 +driver.find_element_by_id("$1_content_content").send_keys("这是Selenium自动化发布的颤颤和抖抖照片")
  46 +sleep(5) # 等待图片上传到图床
  47 +# 发表
  48 +driver.find_element_by_link_text("发表").click()
  49 +
  50 +
  51 +
  52 +
  53 +
  54 +
... ...
Webdriver/frame_test.py 0 → 100644
  1 +++ a/Webdriver/frame_test.py
  1 +from selenium import webdriver
  2 +from time import ctime,sleep
  3 +
  4 +driver = webdriver.Firefox()
  5 +
  6 +file_path = r"E:\Python+Selenium\sc4\Frame.html"
  7 +driver.get(file_path)
  8 +
  9 +driver.switch_to.frame("search")
  10 +driver.find_element_by_css_selector("#query").send_keys("python")
  11 +sleep(3)
  12 +
  13 +driver.find_element_by_css_selector("#stb").click()
  14 +sleep(3)
0 15 \ No newline at end of file
... ...
Webdriver/openweb.py 0 → 100644
  1 +++ a/Webdriver/openweb.py
  1 +from selenium import webdriver
  2 +from time import sleep
  3 +
  4 +#加载浏览器驱动
  5 +driver = webdriver.Firefox()
  6 +
  7 +driver.get("http://www.51zxw.net")
  8 +print(driver.title)
  9 +sleep(3)
  10 +
  11 +driver.get("http://www.baidu.com")
  12 +print(driver.title)
  13 +sleep(2)
  14 +
  15 +# 退出浏览器
  16 +driver.quit()
... ...
Webdriver/switch_to_window.py 0 → 100644
  1 +++ a/Webdriver/switch_to_window.py
  1 +from selenium import webdriver
  2 +from time import sleep
  3 +
  4 +driver = webdriver.Firefox()
  5 +driver.get("http://www.51zxw.net/list.aspx?cid=615")
  6 +selenium_index = driver.current_window_handle
  7 +sleep(2)
  8 +
  9 +driver.find_element_by_partial_link_text("2-1").click()
  10 +sleep(4)
  11 +
  12 +driver.switch_to.window(selenium_index)
  13 +sleep(3)
  14 +
  15 +driver.find_element_by_partial_link_text("3-1").click()
  16 +sleep(3)
  17 +
  18 +driver.quit()
  19 +
  20 +
  21 +
... ...
Webdriver/upload_image.py 0 → 100644
  1 +++ a/Webdriver/upload_image.py
  1 +from selenium import webdriver
  2 +from time import sleep
  3 +
  4 +driver=webdriver.Firefox()
  5 +driver.get("https://www.baidu.com")
  6 +
  7 +driver.find_element_by_css_selector(".soutu-btn").click()
  8 +sleep(3)
  9 +driver.find_element_by_css_selector(".upload-pic").send_keys(r"E:\Python+Selenium\sc4\cat.jpg")
  10 +
  11 +sleep(3)
  12 +# driver.quit()
... ...
Webdriver/webSize.py 0 → 100644
  1 +++ a/Webdriver/webSize.py
  1 +from selenium import webdriver
  2 +from time import sleep
  3 +
  4 +driver=webdriver.Firefox()
  5 +
  6 +driver.get("http://www.51zxw.net")
  7 +driver.maximize_window()
  8 +sleep(2)
  9 +
  10 +driver.get("http://www.51zxw.net/list.aspx?cid=615")
  11 +driver.set_window_size(400,800)
  12 +driver.refresh()
  13 +sleep(2)
  14 +
  15 +driver.back()
  16 +sleep(2)
  17 +
  18 +driver.forward()
  19 +
  20 +sleep(2)
  21 +driver.quit()
... ...
xml_Read/Get_NodeInfo.py 0 → 100644
  1 +++ a/xml_Read/Get_NodeInfo.py
  1 +from xml.dom import minidom
  2 +
  3 +# 打开XML文件
  4 +dom = minidom.parse("class.xml")
  5 +
  6 +root = dom.documentElement
  7 +
  8 +# # 根据标签名称获取标签对象
  9 +# names = root.getElementsByTagName('name')
  10 +# ages = root.getElementsByTagName('age')
  11 +# cities = root.getElementsByTagName('city')
  12 +#
  13 +# # 分别打印显示XML文档标签对中的内容
  14 +# for i in range(4):
  15 +# print(names[i].firstChild.data)
  16 +# print(ages[i].firstChild.data)
  17 +# print(cities[i].firstChild.data)
  18 +
  19 +# 获取属性
  20 +logins = root.getElementsByTagName('login')
  21 +for i in range(2):
  22 + username = logins[i].getAttribute('username')
  23 + password = logins[i].getAttribute('password')
  24 + print(username,password)
0 25 \ No newline at end of file
... ...
xml_Read/class.xml 0 → 100644
  1 +++ a/xml_Read/class.xml
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<Class>
  3 + <student>
  4 + <name>Jack</name>
  5 + <age>28</age>
  6 + <city>Beijing</city>
  7 + </student>
  8 +
  9 + <student>
  10 + <name>Bob</name>
  11 + <age>25</age>
  12 + <city>Shanghai</city>
  13 + </student>
  14 +
  15 + <student>
  16 + <name>Harry</name>
  17 + <age>23</age>
  18 + <city>Shenzhen</city>
  19 + </student>
  20 +
  21 + <teacher>
  22 + <name>Marry</name>
  23 + <age>23</age>
  24 + <city>Changsha</city>
  25 + </teacher>
  26 +
  27 + <account>
  28 + <login username="student" password="123456"/>
  29 + <login username="teacher" password="888888"/>
  30 + </account>
  31 +
  32 +</Class>
0 33 \ No newline at end of file
... ...