我正试图从网页上的"Addresses"选项卡中抓取信息:https://training.gov.au/Organisation/Details/90003,使用的是Python.然而,我遇到了一个问题,即使在指向正确的CSS Select 器或标记之后,代码也只返回空值.奇怪的是,当我指向"摘要"选项卡时,它可以正常工作.网站似乎只返回"摘要"标签的数据.我没有编程经验,所以我不确定是否有需要记住的具体考虑因素.
-
我正在try 从这个网页上的"地址"标签中抓取数据:https://training.gov.au/Organisation/Details/90003.
-
我已经判断了网页,并确定了相关的css Select 器,或作为抓取目标的标签.
-
我正在使用Python进行Web抓取,并try 了像Beautiful Soup和Request库.
-
当我从"摘要"选项卡中抓取数据时,我的代码可以正常工作,但当我试图从"Addresses"选项卡中抓取数据时,它返回空值.
-
我怀疑可能有一些特定的JavaScript或动态内容加载阻止了从"Addresses"选项卡中检索数据.
-
我将不胜感激任何指导如何访问和抓取数据从"地址"标签成功.
代码示例:
以下是我目前用来从"Addresses"选项卡中抓取数据的代码版本:
import requests
from bs4 import BeautifulSoup
# URL of the webpage
url = 'https://training.gov.au/Organisation/Details/90003'
# Send an HTTP GET request to fetch the webpage
response = requests.get(url)
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Parse the HTML content
soup = BeautifulSoup(response.text, 'html.parser')
# Use the CSS selector to target the element with id "rtoDetails-4"
target_element = soup.select_one('#rtoDetails-1') # works for rtoDetails-1 but not other selector
# Check if the element was found
if target_element:
# Extract and print the text content of the element
print(target_element.text.strip())
else:
print("Target element not found.")
else:
print("Failed to retrieve the webpage. Status code:", response.status_code)
预期输出:
我希望rtoDetail-4变量包含来自"Addresses"选项卡的信息,但它目前返回空.
其他信息:
对于如何处理网页上的动态内容或基于JavaScript的加载,任何见解或建议都将不胜感激. 如果我需要遵循特定的步骤,或者如果我遗漏了一些关键的东西,请提供详细的指导,因为我对编码相对较新. 首先感谢您的帮助!