python - 尝试从 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'
解决方案
因为表完全依赖于javascript。我还将 HTMLSession 与 BeautifulSoup 一起使用,并且没有出现任何错误并且没有输出。如果您更喜欢用scrapy 使用硒,那么我可以为您提供帮助。谢谢
推荐阅读
- python - 了解函数 defs 和 __name__ 用途
- bash - 如何使用 docker exec 命令在 CLI 容器中运行 sh 脚本?
- sql - 表单打开的 Microsoft Access 用户日志
- django - 如何使用 Django webpack loader 在 Django 中使用 VueJs 组件?
- javascript - 如何让按键在输入字段上不起作用
- php - 如何从数据库中修复这个 json 编码
- python - 如何从输入变量中相乘?
- ruby-on-rails - 为什么会出现此重复列错误?
- angular - 带布局选项的角度拖放
- logging - 在 django rest 框架中记录用户活动的更好方法