简体   繁体   English

为什么我无法访问 html 中的 Table 容器?

[英]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.

相关问题 如何访问HTML表格单元格中的选择 - How can I access a select in an HTML table cell 为什么我无法从 HTML 文件中访问 javascript function? - Why can't I access javascript function from HTML file? 为什么我不能访问HTML div / jQuery对象的属性? - Why can't I access the properties of a HTML div/jQuery object? Thymeleaf + HTML5-为什么我无法访问我的JavaScript? - Thymeleaf + HTML5 - why can't I access my javascripts? 为什么我无法使用Javascript访问html元素 - Why can't I access my html-elements in Javascript 如何使用 Jquery 将 HTML 元素移到其容器之外? - How can i move HTML elements outside their containers using Jquery? 为什么我不能动态地向HTML表添加行? - Why can't I add rows dynamically to my HTML table? 为什么我无法访问html元素的属性值之一,但是我可以使用jQuery访问其他值? - Why i can't access one of the html element's attribute value, but i can access other ones using jQuery? 如何在HTML表格的特定单元格中访问API数据(Python,请求)? - How can I access API data (Python, Requests) in specific cells of my HTML table? 我如何访问异步并等待 function 之外的 object 属性,然后将其显示到 html 表中 - how can i access the async and await object properties outside of the function and then display it into a html table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM