【发布时间】:2020-07-16 00:39:03
【问题描述】:
我对 Python、JavaScript 和 Web-Scraping 非常陌生。我正在尝试编写将此类表中的所有数据写入 csv 文件的代码。网页是“https://www.mcmaster.com/cam-lock-fittings/material~aluminum/”
我开始尝试在 html 中查找数据,但后来意识到该网站使用 JavaScript。然后我尝试使用 selenium,但我无法在 JavaScript 代码中找到这些表中显示的实际数据的任何地方。我写了这段代码,看看是否可以在任何地方找到显示数据,但我找不到。
from urllib.request import urlopen
from bs4 import BeautifulSoup
from selenium import webdriver
url = 'https://www.mcmaster.com/cam-lock-fittings/material~aluminum/'
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(executable_path='C:/Users/Brian Knoll/Desktop/chromedriver.exe', options=options)
driver.get(url)
html = driver.execute_script("return document.documentElement.outerHTML")
driver.close()
filename = "McMaster Text.txt"
fo = open(filename, "w")
fo.write(html)
fo.close()
我敢肯定,有一个明显的答案在我脑海中浮现。任何帮助将不胜感激!谢谢!
【问题讨论】:
标签: javascript python selenium web-scraping beautifulsoup