Python 2.7 BS4错误';非类型';对象没有属性';查找所有';。无法分析html数据
BS4错误“非类型”对象没有“全部查找”属性。无法分析html数据Python 2.7 BS4错误';非类型';对象没有属性';查找所有';。无法分析html数据,python-2.7,beautifulsoup,Python 2.7,Beautifulsoup,BS4错误“非类型”对象没有“全部查找”属性。无法分析html数据 import requests from bs4 import BeautifulSoup as bs session = requests.session() def get_sizes_in_stock(): global session endpoint = 'https://www.jimmyjazz.com/mens/footwear/nike-air-max-270/AH8050-100?colo
import requests
from bs4 import BeautifulSoup as bs
session = requests.session()
def get_sizes_in_stock():
global session
endpoint = 'https://www.jimmyjazz.com/mens/footwear/nike-air-max-270/AH8050-100?color=White'
response = session.get(endpoint)
soup = bs(response.text,'html.parser')
div = soup.find('div',{'class':'box_wrapper'})
all_sizes = div.find_all('a')
sizes_in_stock = []
for size in all_sizes:
if 'piunavailable' not in size['class']:
size_id = size['id']
sizes_in_stock.append(size_id.split('_')[1])
return sizes_in_stock
print (get_sizes_in_stock())
尝试在headers参数中添加: 更改:
response = session.get(endpoint)
致:
导入请求
从bs4导入BeautifulSoup作为bs
会话=请求。会话()
def获取库存中的尺寸()
全球会议
endpoint=“”
response=session.get(endpoint,headers={'User-Agent':'Mozilla/5.0(Linux;Android 6.0;Nexus 5 Build/MRA58N)AppleWebKit/537.36(KHTML,比如Gecko)Chrome/76.0.3809.100 Mobile Safari/537.36})
打印(在库存()中获取尺寸)1)不要发布代码的照片。发布实际代码,以便用户可以复制错误。2) 出现该错误的原因是它没有返回任何对象。最可能的原因是该站点是动态的,数据是用JavaScription生成的。第三,我刚刚运行了您的代码,对我来说效果很好。输出:
['11438227','11395436','11438228','11395437','11438229','11395438','11438230','11438231','11395439']
该网站是否可能被你方屏蔽?感谢@chitown88的反馈和回复。我不知道这个网站是否因为某种原因被我屏蔽了,它只是不断抛出同样的错误。我正在使用python 2.7。最后,我必须使用selenium加载源页面,然后使用bs4解析数据。hmmm/我使用的是3.6,所以可能就是这样。但是,当所有其他方法都失败时,selenium是一种方法。您也可以尝试在中添加headers
参数。看看下面,让我知道这是否有效。
response = session.get(endpoint, headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'})
soup = bs(response.text,"html.parser")
var = soup.find("var",{"blockwishlist_viewwishlist":"View your wishlist"})
all_sizes = var.find_all("var combinations")
sizes_in_stock = []
for size in all_sizes:
if "0" not in size["quantity"]:
size_id = size["attributes"]
sizes_in_stock.append(size_id)
return sizes_in_stock