You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Selenium提取网站地址标签与JSON数据?JSONObject方案咨询

Answers to Your Selenium & JSON-LD Questions

Hey there! Let's break down your questions one by one, with practical examples you can use right away.

1. How to Extract Address Tags or JSON Values with Selenium WebDriver

From Regular HTML Address Elements

If the address is rendered in standard HTML (like an <address> tag, or divs with specific classes), Selenium's standard locators work perfectly. For example:

  • Grab the full address block: driver.findElement(By.tagName("address")).getText()
  • Target individual components (like street or city) using CSS selectors: driver.findElement(By.cssSelector(".street-address")).getText()

From JSON-LD Scripts

For the application/ld+json script you shared, the process is straightforward:

  1. Locate the script element by its type attribute.
  2. Pull its inner text (this is the raw JSON string).
  3. Parse that string into a JSON object to access any value you need.

Here's a Java code example that ties this together:

// Find the JSON-LD script tag
WebElement jsonScript = driver.findElement(By.cssSelector("script[type='application/ld+json']"));
// Get the raw JSON text
String jsonContent = jsonScript.getText();
// Parse into a JSONObject (using the org.json library)
JSONObject businessData = new JSONObject(jsonContent);

// Extract address details easily
JSONObject address = businessData.getJSONObject("address");
String street = address.getString("streetAddress");
String city = address.getString("addressLocality");
String zip = address.getString("postalCode");

2. Can Selenium Extract the Entire JSON-LD Data? Are There JSONObject Alternatives?

Is This Feasible in Selenium?

100% yes! Selenium can retrieve the full content of the JSON-LD script without any issues. Once you have the raw JSON string, you can parse it into a structured object to access every piece of data—whether you need the whole thing or just specific fields.

Using JSONObject as a Solution

JSONObject (from the org.json library) is actually the go-to approach here for parsing and manipulating the JSON data. It lets you easily extract all data, iterate through keys, or convert the object back to a formatted string.

To get the entire dataset:

// After getting jsonContent as above
JSONObject fullBusinessData = new JSONObject(jsonContent);

// If you want to print the full pretty-printed JSON
System.out.println(fullBusinessData.toString(4));

// Or iterate through all keys to extract every value
Iterator<String> keyIterator = fullBusinessData.keys();
while (keyIterator.hasNext()) {
    String key = keyIterator.next();
    Object value = fullBusinessData.get(key);
    System.out.println(key + ": " + value);
}

This will loop through every top-level key (like name, address, geo) and print their values—including nested objects like the address or opening hours.

If you're using a different language (like Python), you'd use the built-in json module instead of JSONObject, but the core idea is the same: grab the script text, parse it into a dictionary, then access the data.


内容的提问来源于stack exchange,提问作者Aazad R khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:19:02