[英]Why can I not access the Table containers in the html?
I am very new to Python and Web-Scraping.我对 Python 和 Web-Scraping 非常陌生。 I am trying to access the data in all of the tables on this web page and I am unsure why my code is not working.
我正在尝试访问此 web 页面上所有表中的数据,但我不确定为什么我的代码不起作用。 Perhaps something to do with JavaScript and python's inability to read it.
也许与 JavaScript 和 python 无法读取它有关。 My code is:
我的代码是:
from urllib.request import urlopen
from bs4 import BeautifulSoup
import requests
headers = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"}
res = requests.get("https://www.mcmaster.com/cam-lock-fittings/material~aluminum/", headers=headers)
soup = BeautifulSoup(res.text, 'lxml')
item_containers = soup.findAll("div", {"class":"ItmTblCntnr PrsnttnTbl"})
print(len(item_containers))
Any help would be greatly appreciated!任何帮助将不胜感激! Thanks!
谢谢!
Maybe you should try using the html.parser and the content attribute of the response:也许您应该尝试使用 html.parser 和响应的内容属性:
soup = BeautifulSoup(res.content, "html.parser")
By they way which version of Beautiful soup are you using?顺便说一句,你用的是哪个版本的美汤? In mine I have to use find_all instead of findAll.
在我的情况下,我必须使用 find_all 而不是 findAll。
I went ahead and opened up the webpage that you are trying to access with your code.我继续打开了您尝试使用代码访问的网页。 When you see the spinner animation on the page that indicated that it is using JavaScript.
当您在页面上看到微调器 animation 时,表明它正在使用 JavaScript。 When you make a request with the requests library it doesn't execute any JavaScript.
当您使用 requests 库发出请求时,它不会执行任何 JavaScript。 It only receives the html that the server sends.
它只接收服务器发送的 html。 In this case the tables you are trying to access probably dont exist in the initial page load of the web page.
在这种情况下,您尝试访问的表可能不存在于 web 页面的初始页面加载中。 So of you would want to web scrape a webpage like this you would use some browser automation software like selenium.
因此,你们中的一些人想要 web 抓取这样的网页,您将使用一些浏览器自动化软件,例如 selenium。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.