求助:获取Google Play Store当前应用版本的代码失效解决方案
Hey there! I’ve dealt with this exact problem before—Google Play’s web page structure shifts pretty often, so relying on scraping specific HTML elements was always going to be a fragile setup. Let’s walk through how to fix this properly.
- Google regularly updates its Play Store UI, and they’ve removed the
itemprop="softwareVersion"attribute from their page elements. Scraping based on static HTML selectors will inevitably break as these changes happen. - Also, it’s critical to note that scraping Google Play’s web pages violates their Terms of Service. Even if you find a new selector to target, this isn’t a sustainable long-term solution.
1. Use the Google Play Developer API (Official & Reliable)
This is the best long-term approach for fetching your app’s latest version. Here’s how to implement it:
- First, enable the Google Play Developer API in your Google Cloud Console and set up authentication using a service account.
- Then, use the
edits.tracks.getendpoint to pull the version number from your production track. - Example Java snippet (tailored for Android apps):
// Initialize API client with service account credentials GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("service-account-key.json")) .createScoped(Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER)); AndroidPublisher publisher = new AndroidPublisher.Builder( GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), credential ).setApplicationName("Your App Name").build(); // Fetch latest production version code String packageName = "com.your.app.package"; // Note: You'll need to create an edit ID first via the API Track track = publisher.edits().tracks().get(packageName, editId, "production").execute(); String latestVersion = track.getReleases().get(0).getVersionCodes().get(0).toString();
2. Use a Maintained Third-Party Version Check Library
If setting up the official API feels too heavy, use a trusted library that handles version checks reliably (most use compliant methods under the hood):
- Libraries like
AppUpdater(pick a actively maintained fork) can handle version checks, update prompts, and direct users to the Play Store with minimal code. - Just add the dependency to your
build.gradleand configure it with your app’s package name.
3. Temporary Fallback: Scrape the Updated Page (Not Recommended)
If you need a quick fix (and accept the risks of future breaks/TOS violations), the current Play Store page displays the version in a different element. As of now, you can target a span with class htlgb that follows the "Current Version" label. Example JS snippet:
// Temporary web scraping fix (will break eventually) const versionElement = Array.from(document.querySelectorAll('.htlgb')) .find(el => el.previousElementSibling?.textContent.includes('Current Version')); const latestVersion = versionElement?.textContent;
Again, this is a band-aid—Google will change this structure soon, and it’s not compliant with their terms.
Always prioritize the official Google Play Developer API for reliability and compliance. Third-party libraries can save you setup time, but make sure they’re actively maintained. Avoid web scraping unless it’s an absolute last resort.
内容的提问来源于stack exchange,提问作者S.Vishwakarma

