self._current_row += 1
保存文件(这里的’./dj_data.xls’是默认路径,如果调用此函数,没有传file_url参数,则使用’./dj_data.xls’)
def save_file(self, file_url=‘./dj_data.xls’):
try:
self.write_work.save(file_url)
print(“文件保存成功!文件路径为:” + file_url)
except IOError:
print(“文件保存失败!”)
def get_html(url):
模拟浏览器访问
headers = {
‘User-Agent’: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ’
'AppleWebKit/537.36 (KHTML, like Gecko) ’
‘Chrome/81.0.4044.138 Safari/537.36’,
‘accept-language’: ‘zh-CN,zh;q=0.9’
}
print(“–> 正在获取网站信息”)
response = requests.get(url, headers=headers) # 请求访问网站
if response.status_code == 200:
html = response.text # 获取网页源码
return html # 返回网页源码
else:
print(“获取网站信息失