import pyautogui as gui
import pyperclip as pc
import pandas.io.clipboard as cb
import os
import time
gui.PAUSE = 1 # 调用在执行动作后暂停的秒数,只能在执行一些pyautogui动作后才能使用,建议用time.sleep
gui.FAILSAFE = True # 启用自动防故障功能,左上角的坐标为(0,0),将鼠标移到屏幕的左上角,来抛出failSafeException异常
#url地址定位坐标
URL_BAR_X = 300
URL_BAR_Y = 50
#html元素定位坐标
HTML_ELEMEMT_X = 1225
HTML_ELEMEMT_Y = 191
#保存文档目录
SAVE_DIR = "xiaoshuo"
#获取内容
def get_content(url):
pc.copy(url)
gui.click(URL_BAR_X,URL_BAR_Y)
gui.hotkey('ctrl','a')
gui.hotkey('ctrl','v')
gui.keyDown('enter')
time.sleep(1)
gui.click(HTML_ELEMEMT_X,HTML_ELEMEMT_Y,button='right')
gui.moveTo(HTML_ELEMEMT_X+60,HTML_ELEMEMT_Y+36)
gui.mouseDown()
gui.mouseUp()
gui.hotkey('ctrl','a')
gui.hotkey('ctrl','c')
html =cb.paste()
return html
#清理内容
def clean_content(html):
title = html[html.index('<title>')+7:html.index('</title>')]
str = html[:html.index('<main')]
body = html.replace(str,'')
body = body[:body.index('</main>')+7]
html = "<html><head><title>"+title+"</title></head><body>"+body+"</body></html>"
return html
#保存内容
def save_content(fname,html):
if not os.path.exists(SAVE_DIR):
os.mkdir(SAVE_DIR)
file = open(SAVE_DIR+'\\'+fname+'.html', 'w', encoding="UTF-8")
file.write(html)
file = open('urls.txt', encoding='utf-8')
url = file.readline()
i=1
while url:
print(i)
html = get_content(url)
html = clean_content(html)
save_content(str(i),html)
url = file.readline()
i += 1
03-02
772
08-31
7253
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交