Skip to content

Commit

Permalink
Merge pull request #147 from ljzloser/dev
Browse files Browse the repository at this point in the history
删除headers,加入浏览器的标头会导致WOM读取的数据错误,修改世界扫雷网的解析逻辑,使得精准匹配对应的数据
  • Loading branch information
eee555 authored Oct 19, 2024
2 parents 6027943 + 0a44f8a commit 16e078c
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions back_end/saolei/accountlink/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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:
# 请求超时
Expand All @@ -130,14 +125,12 @@ 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()')
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()')
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()')
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):
Expand All @@ -151,7 +144,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:
# 请求超时
Expand Down

0 comments on commit 16e078c

Please sign in to comment.