执行Python程序无输出、黑屏问题求助(Android开发者)
Hey there! As someone who’s dabbled in both Android development and web scraping, let’s break down why your Python script is giving you a black screen with no output, and fix it step by step.
1. Missing User-Agent Header (Most Likely Culprit)
Most government websites like parivahan block requests from the default requests library User-Agent—it’s a dead giveaway for bots. Let’s add a realistic browser UA to your requests to avoid being blocked:
import sys import requests from bs4 import BeautifulSoup, SoupStrainer # Add a browser-like User-Agent headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36' } home_url = 'https://parivahan.gov.in/rcdlstatus/' post_url = 'https://parivahan.gov.in/rcdlstatus/vahan/rcDlHome.xhtml' # ... rest of your code ... # Update GET request with headers r = requests.get(url=home_url, headers=headers) # ... rest of your code ... # Update POST request with headers r = requests.post(url=post_url, data=data, cookies=cookies, headers=headers)
2. Dynamic Form IDs (The Hardcoded j_idt32 Problem)
JSF-based sites like parivahan generate dynamic IDs for form elements every time the page loads. Your hardcoded form_rcdl:j_idt32 might not match the current page’s ID. Let’s extract it dynamically:
# After parsing the initial GET response submit_button = soup.select_one('input[type="submit"][id^="form_rcdl:j_idt"]') if not submit_button: print("Error: Couldn't find the submit button ID") sys.exit(1) button_id = submit_button['id'] # Update your data dictionary with the dynamic ID data = { 'javax.faces.partial.ajax':'true', 'javax.faces.source': button_id, 'javax.faces.partial.execute':'@all', 'javax.faces.partial.render': 'form_rcdl:pnl_show form_rcdl:pg_show form_rcdl:rcdl_pnl', button_id: button_id, 'form_rcdl':'form_rcdl', 'form_rcdl:tf_reg_no1': first, 'form_rcdl:tf_reg_no2': second, 'javax.faces.ViewState': viewstate, }
3. Add Argument Validation (Avoid Silent Crashes)
Since you’re using command-line arguments, a missing input will crash the script silently if you don’t add checks. Let’s add a quick validation at the start:
if len(sys.argv) != 3: print("Usage: python your_script.py <first_part> <last_four_digits>") print("Example: python your_script.py GJ03KA 0803") sys.exit(1) first = sys.argv[1] second = sys.argv[2]
4. Debugging Steps to See What’s Happening
Add print statements to verify if your requests are succeeding and what content you’re getting back:
# After the initial GET request print(f"GET Request Status: {r.status_code}") if r.status_code != 200: print("Error: Failed to load the homepage") sys.exit(1) # After the POST request print(f"POST Request Status: {r.status_code}") print(f"POST Response Preview: {r.text[:500]}") # Print first 500 characters of the response
- A
403status code means you’re being blocked (the User-Agent fix should resolve this). - A
200status with empty or JS-heavy content might mean you need to use a tool like Selenium to handle dynamic rendering, but let’s try the simpler fixes first.
5. Verify ViewState Extraction
Make sure you’re correctly grabbing the ViewState (it might be missing or in a different element):
viewstate_elements = soup.select('input[name="javax.faces.ViewState"]') if not viewstate_elements: print("Error: Couldn't find ViewState") sys.exit(1) viewstate = viewstate_elements[0]['value']
Try implementing these fixes one by one—start with the User-Agent header, as that’s the most common issue with government site scraping.
内容的提问来源于stack exchange,提问作者Shubham Sejpal

