五一小长假,在家除了吃饭、睡觉、打豆豆还做什么呢?
刷抖音呀....笨蛋。。。
O(∩_∩)O哈哈~
那还不如给自己的博客刷几百的浏览量呢,嘻嘻~~
import random
import functools
import time
import logging
import os
from selenium import webdriver
log_file = os.path.join(os.getcwd() + "/request.log")
logging.basicConfig(level=logging.INFO,
filename=log_file,
datefmt='%Y-%m-%d %H:%M:%S',
format='%(asctime)s %(levelname)s %(module)s %(message)s')
Logger = logging.getLogger(__name__)
def run_time(func):
@functools.wraps(func)
def wraps(*args, **kwargs):
start = time.perf_counter()
result = func(*args, **kwargs)
end = time.perf_counter()
Logger.info("使用装饰器方法:{}, 响应时间:{}ms, 输出的结果:{}".format(func.__name__, int((end - start) * 1000), result))
return wraps
def parse01():
url = ("https://www.jianshu.com/p/5af7da67a01b", "https://www.jianshu.com/p/3bdd46169675",
"https://www.jianshu.com/p/cf50b24035c8", "https://www.jianshu.com/p/6dbd42408b17",
"https://www.jianshu.com/p/edc0119175a7")
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) "
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36"
}
url_single = random.choice(url)
print("请求的url是:", url_single)
return url_single, headers
def selenium_request(n):
options = webdriver.ChromeOptions()
# 添加无界面参数
options.add_argument('--headless')
url, headers = parse01()
driver = webdriver.Chrome()
try:
driver.get(url)
time.sleep(2)
driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')
cookie = driver.get_cookies()
driver.maximize_window()
driver.save_screenshot("./%.2d__jianshu.png" % n)
size = driver.get_window_size()
return cookie, size
except Exception as error:
print(error)
finally:
time.sleep(1)
driver.quit()
@run_time
def main(n):
a = selenium_request(n)
return a
if __name__ == '__main__':
for n in range(1, 101):
print("第", n, "次请求")
main(n)
beautiful.jpg
网友评论