diff --git a/scraper.py b/scraper.py index d0e9441..d5bb89b 100644 --- a/scraper.py +++ b/scraper.py @@ -82,6 +82,15 @@ class Scraper: except Exception as e: print('Error in get_url:', e) return None + + @staticmethod + def relpath(file): + """ Always locate to the correct relative path. """ + from sys import _getframe + from pathlib import Path + frame = _getframe(1) + curr_file = Path(frame.f_code.co_filename) + return str(curr_file.parent.joinpath(file).resolve()) # 生成X-Bogus签名/Generate X-Bogus signature @staticmethod @@ -196,7 +205,7 @@ class Scraper: """ # 调用JavaScript函数 query = urllib.parse.urlparse(url).query - xbogus = execjs.compile(open('./X-Bogus.js').read()).call('sign', query, self.headers['User-Agent']) + xbogus = execjs.compile(open(self.relpath('./X-Bogus.js')).read()).call('sign', query, self.headers['User-Agent']) print('生成的X-Bogus签名为: {}'.format(xbogus)) new_url = url + "&X-Bogus=" + xbogus return new_url