-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmybasespider.py
More file actions
28 lines (23 loc) · 968 Bytes
/
Copy pathmybasespider.py
File metadata and controls
28 lines (23 loc) · 968 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# coding=utf-8
import requests
import time
class MyBaseSpider(object):
"""自定义爬虫基类"""
def __init__(self, my_log_file_name="my.log"):
# 请求头
self.headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
}
# 日志文件
self.f = open(my_log_file_name, "w")
def __del__(self):
self.f.close()
def my_log(self, msg):
"""记录一些日志信息"""
self.f.write("[{}] {}\n".format(time.strftime("%Y/%m/%D %H:%M:%S", time.localtime()), msg))
self.f.flush()
def my_get(self, url, stream=False):
"""用于发送get请求的函数,可以处理一些反爬,还有打印一些信息"""
response = requests.get(url, headers=self.headers, stream=stream)
print("{} [{}]".format(response.url, response.status_code))
return response