From 97e278aff7a51194d16eaf3bff0c19a0fe711da0 Mon Sep 17 00:00:00 2001 From: LjingZhijoin Group <107012527+ljzloser@users.noreply.github.com> Date: Sat, 19 Oct 2024 18:52:07 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=88=A0=E9=99=A4headers,=E5=8A=A0?= =?UTF-8?q?=E5=85=A5=E6=B5=8F=E8=A7=88=E5=99=A8=E7=9A=84=E6=A0=87=E5=A4=B4?= =?UTF-8?q?=E4=BC=9A=E5=AF=BC=E8=87=B4WOM=E8=AF=BB=E5=8F=96=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E9=94=99=E8=AF=AF=EF=BC=8C=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=B8=96=E7=95=8C=E6=89=AB=E9=9B=B7=E7=BD=91=E7=9A=84=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E9=80=BB=E8=BE=91=EF=BC=8C=E4=BD=BF=E5=BE=97=E7=B2=BE?= =?UTF-8?q?=E5=87=86=E5=8C=B9=E9=85=8D=E5=AF=B9=E5=BA=94=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back_end/saolei/accountlink/utils.py | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/back_end/saolei/accountlink/utils.py b/back_end/saolei/accountlink/utils.py index 0e454b1..c96e415 100644 --- a/back_end/saolei/accountlink/utils.py +++ b/back_end/saolei/accountlink/utils.py @@ -5,11 +5,6 @@ import requests from lxml import etree - -headers = { - 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36' -} - def update_account(platform: Platform, id, user: UserProfile | None): if platform == Platform.SAOLEI: update_saolei_account(id, user) @@ -41,12 +36,12 @@ def update_saolei_account(id, user: UserProfile | None): VideoHtmlStr = None try: url = f'http://saolei.wang/Player/Info.asp?Id={id}' - response = requests.get(url=url, timeout=5,headers=headers) + response = requests.get(url=url, timeout=5) response.encoding = 'GB2312' InfoHtmlStr = response.text url = f'http://saolei.wang/Video/Satus.asp?Id={id}' - response = requests.get(url=url, timeout=5,headers=headers) + response = requests.get(url=url, timeout=5) response.encoding = 'GB2312' VideoHtmlStr = response.text @@ -118,7 +113,7 @@ def update_msgames_account(id, user: UserProfile | None): htmlStr = None try: url = f'https://minesweepergame.com/profile.php?pid={id}' - response = requests.get(url=url, timeout=5, headers=headers) + response = requests.get(url=url, timeout=5) htmlStr = response.text except requests.exceptions.Timeout as e: # 请求超时 @@ -130,13 +125,13 @@ def update_msgames_account(id, user: UserProfile | None): # 没有爬取到信息 return tree = etree.HTML(htmlStr) - values = tree.xpath('/html/body/div[3]/div/div/div/div[2]/table/tr[1]/td[2]/text()') + values = tree.xpath('//td[text()="Name"]/../td[2]/text()') account.name = values[0] if values else None - values = tree.xpath('/html/body/div[3]/div/div/div/div[2]/table/tr[2]/td[2]/text()') + values = tree.xpath('//td[text()="Local Name"]/../td[2]/text()') account.local_name = values[0] if values else None - values = tree.xpath('/html/body/div[3]/div/div/div/div[2]/table/tr[6]/td[2]/text()') + values = tree.xpath('//td[text()="Joined"]/../td[2]/text()') account.joined = values[0] if values else None account.save() @@ -151,7 +146,7 @@ def update_wom_account(id, user: UserProfile | None): account.update_time = datetime.now() htmlStr = None try: - response = requests.get(url=url, timeout=5, headers=headers) + response = requests.get(url=url, timeout=5) htmlStr = response.text except requests.exceptions.Timeout as e: # 请求超时 From 0a44f8a74234e04676e39754618056a3cb390123 Mon Sep 17 00:00:00 2001 From: LjingZhijoin Group <107012527+ljzloser@users.noreply.github.com> Date: Sat, 19 Oct 2024 19:12:30 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=B0=86=E4=B8=96=E7=95=8C=E6=89=AB?= =?UTF-8?q?=E9=9B=B7=E7=BD=91=E7=9A=84=E8=A7=A3=E6=9E=90=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E6=8A=BD=E8=B1=A1=E5=87=BA=E6=9D=A5=EF=BC=8C=E4=BD=BF=E5=BE=97?= =?UTF-8?q?=E5=90=8E=E7=BB=AD=E6=B7=BB=E5=8A=A0=E6=9F=90=E4=BA=9B=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E6=97=B6=E6=9B=B4=E7=AE=80=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back_end/saolei/accountlink/utils.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/back_end/saolei/accountlink/utils.py b/back_end/saolei/accountlink/utils.py index c96e415..0c7720b 100644 --- a/back_end/saolei/accountlink/utils.py +++ b/back_end/saolei/accountlink/utils.py @@ -125,14 +125,12 @@ def update_msgames_account(id, user: UserProfile | None): # 没有爬取到信息 return tree = etree.HTML(htmlStr) - values = tree.xpath('//td[text()="Name"]/../td[2]/text()') - account.name = values[0] if values else None - - values = tree.xpath('//td[text()="Local Name"]/../td[2]/text()') - account.local_name = values[0] if values else None - - values = tree.xpath('//td[text()="Joined"]/../td[2]/text()') - account.joined = values[0] if values else None + def getValue(text): + values = tree.xpath(f'//td[text()="{text}"]/../td[2]/text()') + return values[0] if values else None + account.name = getValue('Name') + account.local_name = getValue('Local Name') + account.joined = getValue('Joined') account.save() def update_wom_account(id, user: UserProfile | None):