Python网页爬取问题:Requests获取内容缺失目标标签
Hey there! I totally get where you're stuck—dealing with JavaScript-rendered content when you're just starting out with Python web scraping can be tricky. Let's break down what's happening and how you can fix this.
Why Your Current Code Isn't Working
When you use requests.get(), you're only fetching the static HTML of the page. The warranty info you're looking for is loaded dynamically by JavaScript after the initial page loads. BeautifulSoup can't execute JavaScript, so it never sees those elements added later.
Fix 1: Use Selenium to Simulate a Real Browser
Selenium lets you control a web browser programmatically, which means it will run all the JavaScript on the page and render the full content just like your regular browser does. Here's how to implement it:
Step 1: Install Required Packages
First, install Selenium and download the appropriate driver for your browser (e.g., ChromeDriver for Google Chrome):
pip install selenium
Make sure the driver executable is in your system PATH or specify its path in the code.
Step 2: Updated Code with Selenium
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Initialize Chrome browser (you can use Firefox, Edge, etc. too) options = webdriver.ChromeOptions() options.add_argument('--headless=new') # Run in background without opening a window driver = webdriver.Chrome(options=options) try: my_url = 'https://www.dell.com/support/home/ca/en/cadhs1/product-support/servicetag/0-NE9lVXI4NlpmbjFtRHJBbTF0dDhoQT090/overview' driver.get(my_url) # Wait for the warranty element to load (adjust timeout if needed) warranty_element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, 'warrantyExpiringLabel')) ) # Extract the text warranty_expiry = warranty_element.text print(f"Warranty Expires: {warranty_expiry}") finally: # Always close the browser when done driver.quit()
Fix 2: Fetch Data Directly from Dell's API (More Efficient)
Instead of loading the entire page, you can find the API endpoint that Dell uses to fetch warranty data. This is faster and more reliable than simulating a browser. Here's how to find it:
- Open your browser's DevTools (F12) and go to the Network tab.
- Refresh the Dell support page.
- Look for XHR/fetch requests (filter by "XHR" in the Network tab).
- Search for requests that include terms like "warranty" or "support". You'll likely find a JSON response with all the warranty details.
Once you have the API URL, you can use requests to fetch the data directly:
import requests # Replace with the actual API endpoint you found api_url = "https://api.dell.com/support/assetinfo/v4/getassetwarranty" params = { "servicetags": "YOUR_SERVICE_TAG", "apikey": "YOUR_API_KEY" # You might need to register for a Dell API key } headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36" } response = requests.get(api_url, params=params, headers=headers) warranty_data = response.json() # Extract expiry date from the JSON (structure will vary based on the API response) expiry_date = warranty_data['AssetWarrantyResponse'][0]['Warranties'][0]['EndDate'] print(f"Warranty Expires: {expiry_date}")
Note: Dell's public API might require an API key—you can get one by registering on their developer portal.
Next Steps for Storing in Zabbix
Once you have the warranty data, you can push it to Zabbix using:
- Zabbix API: Use
requeststo send POST requests to Zabbix's API to create items or update host metadata. - Zabbix Sender: Use the
zabbix_sendercommand-line tool (or a Python wrapper likepyzabbix) to send data directly to your Zabbix server.
Don't worry if this feels overwhelming at first—web scraping with dynamic content takes a bit of practice, but you're already on the right track!
内容的提问来源于stack exchange,提问作者Peter Franca

