我正在使用bs4
抓取khanacademy上的https://www.khanacademy.org/profile/DFletcher1990/ one用户资料。
我正在尝试获取用户统计数据(加入日期,获得的能量点,完成的视频)。
我有check https://www.crummy.com/software/BeautifulSoup/bs4/doc/
这似乎是:“最常见的意外行为是找不到您知道在文档中的标记。您看到它进入,但find_all()
返回[]
或find()
返回None
。这是Python内置解析器的另一个常见问题,它有时会跳过它不理解的标记。同样,解决方案是安装lxml或html5lib。”
我尝试了不同的解析器方法,但我得到了相同的问题。
from bs4 import BeautifulSoup
import requests
url = 'https://www.khanacademy.org/profile/DFletcher1990/'
res = requests.get(url)
soup = BeautifulSoup(res.content, "lxml")
print(soup.find_all('div', class_='profile-widget-section'))
我的代码返回[]
。
发布于 2019-02-15 20:04:29
使用javascript加载页面内容。检查内容是否是动态的最简单的方法是右键单击并查看页面源代码,然后检查内容是否存在。您也可以在浏览器中关闭javascript,然后转到url。
您可以使用selenium获取内容
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup
driver = webdriver.Chrome(executable_path='/path/to/chromedriver')
driver.get("https://www.khanacademy.org/profile/DFletcher1990/")
element=WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH ,'//*[@id="widget-list"]/div[1]/div[1]/div[2]/div/div[2]/table')))
source=driver.page_source
soup=BeautifulSoup(source,'html.parser')
user_info_table=soup.find('table', class_='user-statistics-table')
for tr in user_info_table.find_all('tr'):
tds=tr.find_all('td')
print(tds[0].text,":",tds[1].text)
输出:
Date joined : 4 years ago
Energy points earned : 932,915
Videos completed : 372
另一个可用的选择(因为您已经熟悉了请求)是使用requests-html
from bs4 import BeautifulSoup
from requests_html import HTMLSession
session = HTMLSession()
r = session.get('https://www.khanacademy.org/profile/DFletcher1990/')
r.html.render(sleep=10)
soup=BeautifulSoup(r.html.html,'html.parser')
user_info_table=soup.find('table', class_='user-statistics-table')
for tr in user_info_table.find_all('tr'):
tds=tr.find_all('td')
print(tds[0].text,":",tds[1].text)
输出
Date joined : 4 years ago
Energy points earned : 932,915
Videos completed : 372
另一种选择是找出正在发出的ajax请求,并模拟该请求并解析响应。此响应不必总是json。但在这种情况下,内容不会通过ajax响应发送到浏览器。它已经存在于页面源代码中。
该页面简单地使用javascript来组织这些信息。您可以尝试从脚本标记中获取数据,这可能涉及到一些正则表达式,然后从字符串中生成一个json。
https://stackoverflow.com/questions/54714668
复制