首页 > 解决方案 > 尝试从 Python BS 中的网站抓取表格时出现 AttributeError

问题描述

我不确定这个片段是否适用于这个 url 或页面。我尝试在 url 中运行脚本,但出现 AttributeError。

from selenium import webdriver
import requests
from bs4 import BeautifulSoup

browser = webdriver.Chrome('C://Python38/chromedriver')
browser.get("https://poocoin.app/rugcheck/0xe56842ed550ff2794f010738554db45e60730371/top-holders")
url = "https://poocoin.app/rugcheck/0xe56842ed550ff2794f010738554db45e60730371/top-holders"

r = requests.get(url)
soup = BeautifulSoup(r.text, 'lxml')
t = soup.find('table', class_='table table-bordered table-condensed text-small')
trs = t.find('tbody').find_all('tr')
for tr in trs[:10]:
    print(list(tr.stripped_strings))
browser.quit()

当前输出/错误:

    Traceback (most recent call last):
    File "C:/Users/Acer/poocoin.py", line 8, in <module>
    trs = t.find('tbody').find_all('tr')
    AttributeError: 'NoneType' object has no attribute 'find'

标签: pythonpython-3.xbeautifulsouprequest

解决方案


因为表完全依赖于javascript。我还将 HTMLSession 与 BeautifulSoup 一起使用,并且没有出现任何错误并且没有输出。如果您更喜欢用scrapy 使用硒,那么我可以为您提供帮助。谢谢


推荐阅读