You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python TypeError: 'NoneType'对象不可订阅问题求助

Presearch搜索脚本TypeError错误修复方案

问题原因

你遇到的TypeError: 'NoneType' object is not subscriptable错误,是因为代码第13行的soup.find("input", {"name": "_token"})没有找到目标元素,返回了None,之后强行访问["value"]就触发了报错。常见诱因有两个:

  • Presearch的页面结构更新,_token的存储位置或元素属性变了
  • 请求头过于简单,被网站反爬机制拦截,返回的不是正常首页内容

修复步骤

1. 先确认Token的实际位置

在初始化soup后加入一行代码,打印返回的页面内容,查看_token的真实载体:

print(content.decode('utf-8'))

如果发现_token放在meta标签里(比如<meta name="_token" content="xxx">),就把提取逻辑改成:

token_meta = soup.find("meta", {"name": "_token"})
if token_meta:
    token = token_meta.get("content")
else:
    print("找不到_token,请检查页面结构")
    exit()

2. 完善请求头,规避反爬

默认的requests请求头容易被识别为爬虫,添加常见的浏览器标识:

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Language': 'en-US,en;q=0.5',
    'Referer': 'https://www.presearch.org/'
}

之后所有get/post请求都带上这个headers。

3. 强制添加空值检查

不管页面结构怎么变,先判断元素是否存在再访问属性,避免崩溃:

token_input = soup.find("input", {"name": "_token"})
if not token_input:
    # 备用方案:尝试从meta标签取token
    token_meta = soup.find("meta", {"name": "_token"})
    if token_meta:
        token = token_meta.get("content")
    else:
        print("无法获取_token,请检查页面或请求头")
        exit()
else:
    token = token_input["value"]

4. 修复循环逻辑错误

原代码里的搜索请求写在循环外面,只会执行一次搜索,要把相关代码缩进进循环:

for x in range(0, 10):
    words = random.choice(search_words)
    payload = "term={}&provider_id=98&_token={}".format(words, token)
    r.post("https://www.presearch.org/search", data=payload, headers=login_headers)
    print(f"Term:{words} Search done!")
    time.sleep(10)

5. 余额检查也加防护

同样,余额元素也可能找不到,提前做判断:

balance = soup_balance.find("span", {"class": "number ajax balance"})
if balance:
    print(f"Your Balance: {balance.text.strip()} PRE")
else:
    print("无法获取余额,请检查登录状态或页面结构")

完整修复代码

import time
import requests
from bs4 import BeautifulSoup
import random

email = "Enter your email"
password = "Enter your password"

# 初始化会话和请求头
r = requests.Session()
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Language': 'en-US,en;q=0.5',
    'Referer': 'https://www.presearch.org/'
}

# 获取首页内容并提取token
content = r.get("https://www.presearch.org", headers=headers).content
soup = BeautifulSoup(content, 'html.parser')

token_input = soup.find("input", {"name": "_token"})
if not token_input:
    # 尝试从meta标签找token(适配页面更新)
    token_meta = soup.find("meta", {"name": "_token"})
    if token_meta:
        token = token_meta.get("content")
    else:
        print("无法找到_token,请检查页面结构或请求头")
        exit()
else:
    token = token_input["value"]

# 登录请求
payload_login = "_token={}&login_form=1&email={}&password={}".format(token, email, password)
login_headers = headers.copy()
login_headers['Content-Type'] = 'application/x-www-form-urlencoded'
login = r.post("https://www.presearch.org/api/auth/login", data=payload_login, headers=login_headers)

# 执行10次搜索
search_words = ["apple", "life", "hacker", "facebook", "abeyancies", "abeyancy", "abeyant", "abfarad", "abfarads", "abhenries",
                "abhenry", "abhenrys", "abhominable", "abhor", "abhorred", "abhorrence", "abhorrences", "abhorrencies",
                "abhorrency", "abhorrent", "abhorrently", "abhorrer", "abhorrers", "abhorring", "abhorrings", "abhors", "abid",
                "abidance", "abidances", "abidden", "abide", "abided", "abider", "abiders", "abides", "abiding", "abidingly",
                "abidings", "abies", "abietic", "abigail", "abigails", "abilities", "ability", "abiogeneses", "abiogenesis",
                "abiogenetic", "abiogenetically", "abiogenic", "abiogenically", "abiogenist", "abiogenists", "abiological",
                "abioses", "abiosis", "abiotic", "abiotically", "abiotrophic", "abiotrophies", "abiotrophy"]

for x in range(0, 10):
    words = random.choice(search_words)
    payload_search = "term={}&provider_id=98&_token={}".format(words, token)
    r.post("https://www.presearch.org/search", data=payload_search, headers=login_headers)
    print(f"Term:{words} Search done!")
    time.sleep(10)

# 获取余额
balance_page = r.get("https://www.presearch.org/", headers=headers)
soup_balance = BeautifulSoup(balance_page.content, 'html.parser')
balance = soup_balance.find("span", {"class": "number ajax balance"})

if balance:
    print(f"Your Balance: {balance.text.strip()} PRE")
else:
    print("无法获取余额,请检查页面结构或登录状态")

内容的提问来源于stack exchange,提问作者Mluka23

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 03:01:12