|
| 1 | +import shutil |
| 2 | + |
| 3 | +import requests |
| 4 | +from selenium.webdriver import PhantomJS |
| 5 | +from selenium.webdriver.common.desired_capabilities import DesiredCapabilities |
| 6 | + |
| 7 | +from configs import COOKIES, HEDADERS |
| 8 | + |
| 9 | + |
| 10 | +def my_session(): |
| 11 | + session = requests.Session() |
| 12 | + session.get('https://www.zhihu.com', |
| 13 | + cookies=COOKIES, headers=HEDADERS) |
| 14 | + return session |
| 15 | + |
| 16 | + |
| 17 | +def get_image(url, path): |
| 18 | + res = requests.get(url, stream=True) |
| 19 | + with open(path, 'wb') as f: |
| 20 | + shutil.copyfileobj(res.raw, f) |
| 21 | + |
| 22 | + |
| 23 | +def save_html(text, name): |
| 24 | + with open(name, 'w') as f: |
| 25 | + f.write(text) |
| 26 | + |
| 27 | + |
| 28 | +def get_driver(): |
| 29 | + # 设置请求头 |
| 30 | + dcap = dict(DesiredCapabilities.PHANTOMJS) |
| 31 | + dcap["phantomjs.page.settings.userAgent"] = (HEDADERS.get("User-Agent")) |
| 32 | + # 初始化driver |
| 33 | + driver = PhantomJS(desired_capabilities=dcap) |
| 34 | + # 加入cookies |
| 35 | + for c in my_session().cookies: |
| 36 | + driver.add_cookie({'name': c.name, 'value': c.value, |
| 37 | + 'path': c.path, 'expiry': c.expires, 'domain': c.domain}) |
| 38 | + # 设置窗口大小 |
| 39 | + driver.set_window_position(0, 0) |
| 40 | + driver.set_window_size(1920, 1080) |
| 41 | + return driver |
0 commit comments