即使元素存在,BeautifulSoup 也返回 None

时间:2021-02-24 21:40:44

标签: python web-scraping beautifulsoup

我正在尝试从该网站上抓取表格: https://maya.tase.co.il/funds?view=all

当我查看后端代码时,我发现了第一个单元格的类(从右到左): tableCol col_1 ng-scope

现在,我的代码如下:

import requests
from bs4 import BeautifulSoup


URL = 'https://maya.tase.co.il/funds?view=all'
page = requests.get(URL)

soup = BeautifulSoup(page.content, 'html.parser')

results = soup.find('div',{'class':'tableCol col_1 ng-scope'})

print(results)

但是当运行上面的我得到 None 结果。 当我尝试不同的元素时也是这种情况。 任何人都知道可能的解决方案吗? 不知道我做错了什么。

提前致谢, 乌里

1 个答案:

答案 0 :(得分:0)

试试这个:

from bs4 import BeautifulSoup as soup
from selenium import webdriver
import time
url = 'https://maya.tase.co.il/funds?view=all'
driver = webdriver.Chrome()
driver.get(url)
time.sleep(10)
soup_page=driver.page_source
soup_page=soup(driver.page_source,'html.parser')
time.sleep(20)
results=soup_page.find('div',{'class':'tableCol col_1 ng-scope'}).text
print(results)

time.sleep() 用于让页面正确加载。