Python beautifulsoup-获取输入值/ TypeError:'NoneType'对象不可下标

时间:2019-05-24 06:54:53

标签: python web-scraping beautifulsoup

试图找到formkey但出现错误:

TypeError: 'NoneType' object is not subscriptable

this webpage上。

需要查找值。 我在寻找什么可以找到 在查看源:https://wellgosh.com/customer/account/create/和control + f name =“ form_key” value =

formkey_acc = soup.find('input', {'name': 'form_key'})['value']
s = requests.session()

def c_acc():
    acc = s.get('https://wellgosh.com/customer/account/create/')
    soup = bs(acc.text, 'html.parser')
    formkey_acc = soup.find('input', {'name': 'form_key'})['value']
    print(formkey_acc)
formkey_acc = soup.find('input', {'name': 'form_key'})['value']

TypeError:“ NoneType”对象不可下标

1 个答案:

答案 0 :(得分:0)

它们是请求的默认用户代理上的403'...

import requests
from bs4 import BeautifulSoup as bs

def c_acc(s):
    acc = s.get(
        'https://wellgosh.com/customer/account/create/',
        headers={'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36"}
    )
    soup = bs(acc.text, 'html.parser')
    formkey_acc = soup.find('input', {'name': 'form_key'}).get('value')
    print(formkey_acc)

s = requests.session()
c_acc(s)