"""
python获取txt中多行网址(每行一个网址)并网页截图v202111302258(原创)
chromedriver.exe版本号与chrome.exe版本号(地址栏输入chrome://version/查看版本)相同,放于python.exe同级目录(C:\Program Files (x86)\Python310-32);下载地址:https://chromedriver.storage.googleapis.com/index.html
"""
from selenium import webdriver
from urllib import request
import re
import os
import datetime
print(datetime.datetime.now())
filedir = "d:\\tsldrb_pic\\"
if not os.path.exists(filedir):
os.makedirs(filedir)
f = open("d:\\urls.txt", "r", encoding='utf-8')
urls = f.readlines()
for url in urls:
print(url.replace('\n', ''))
driver = webdriver.Chrome()
driver.maximize_window()
driver.get(url)
data_content = request.urlopen(url.replace('\n', ''))
content_text = data_content.read().decode()
result_title_time = re.search('(?<=content/).*(?=/content)', url.replace('\n', '')) # 匹配content/和/content间时间
result_title = re.search('(?<=">).*(?=</h2)', content_text) # 匹配">和</h2间的标题
result_title_time.group().replace('/', '')
pic_name = result_title_time.group().replace('/', '') + result_title.group().replace(' ', '') + '.png'
driver.get_screenshot_as_file(filedir + pic_name)
driver.close()
print(datetime.datetime.now())