Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

可以在search中添加用户身份认证信息吗,为什么返回空值呢 #499

Open
cheneylqy opened this issue Aug 12, 2024 · 1 comment

Comments

@cheneylqy
Copy link

def get_user_verified_info(self, bid):
url = f"https://weibo.com/ajax/statuses/show?id={bid}&locale=zh-CN"
headers = self.settings.get('DEFAULT_REQUEST_HEADERS')
# 添加详细日志
logging.info(f"Requesting user info for bid: {bid}")
logging.info(f"Request URL: {url}")
logging.info(f"Request headers: {headers}")

    try:
        response = requests.get(url, headers=headers)
        response.raise_for_status()  # 如果响应状态不是200,将引发异常
    except requests.RequestException as e:
        logging.error(f"Request failed for bid {bid}: {e}")
        return False, ""

    logging.info(f"Response status code: {response.status_code}")
    logging.info(f"Response content: {response.text[:1000]}...")  # 记录响应内容的前1000个字符

    try:
        data = response.json()
    except json.JSONDecodeError:
        logging.error(f"Failed to parse JSON for bid {bid}")
        return False, ""

    user = data.get("user", {})
    verified = user.get("verified", False)
    verified_reason = user.get("verified_reason", "")

    logging.info(f"Extracted verified info for bid {bid}: verified={verified}, reason={verified_reason}")
    return verified, verified_reason  #修改后

然后在 def parse_weibo(self, response):中添加
# 提取 bid 并获取认证信息
bid = sel.xpath('.//div[@Class="from"]/a[1]/@href').extract_first().split('/')[-1].split('?')[0]
weibo['bid'] = bid
verified, verified_reason = self.get_user_verified_info(bid)
weibo['verified'] = verified
weibo['verified_reason'] = verified_reason

为什么最后运行时verified_reason还是空值呢,求解答

@dataabc
Copy link
Owner

dataabc commented Aug 12, 2024

可能网页改变了,导致xpath没有获取成功,您可以利用一些xpath工具获取再修改代码。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants