汇报一下:用 Selenium 可以解决,本地配了 Chromedriver 之后,如下代码解君愁:
from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_optiOns= Options() chrome_options.add_argument('--headless') chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--disable-dev-shm-usage') chrome_options.add_argument("window-size=1920,1080") d = webdriver.Chrome(chrome_optiOns=chrome_options) d.get('http://www.v2ex.com') elems = d.find_elements_by_class_name('topic-link') print(len(elems), 'elements found') for i, elem in enumerate(elems): print(i + 1, '/', len(elems), 'location:', elem.location, 'size:', elem.size, 'content:', elem.text)
输出如下:
49 elements found 1 / 49 location: {'x': 490, 'y': 162} size: {'height': 17, 'width': 205} content: WSL 2 拳打 macOS,脚踢 Ubuntu ? 2 / 49 location: {'x': 490, 'y': 233} size: {'height': 17, 'width': 90} content: 想问下有 Pixel 用户吗? 3 / 49 location: {'x': 490, 'y': 304} size: {'height': 17, 'width': 246} content: Windows Subsystem for Android 来了 4 / 49 location: {'x': 490, 'y': 375} size: {'height': 17, 'width': 247} content: Google Authenticator 更新了,之前重复的两步校验消失 ...