use url join import requests from parsel import Selector from lib.dbdog import DBdog…
- User
use url join import requests from parsel import Selector from lib.dbdog import DBdog from diskcache import Cache import time import json
UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36'
dbd = DBdog() # Initialize DBdog instance
cache = Cache(directory='cache')
@cache.memoize() def fetch_url(url): response = requests.get(url, headers={'user-agent': UA}, timeout=10) response.raise_for_status()
return response.textdef get_results(url): html_content = fetch_url(url) # Use parsel to select and extract data # Parse the HTML content selector = Selector(text=html_content)
# Initialize a dictionary to hold the results cities = [] # Extract headings and corresponding cities for section in selector.css(".row.colbtm"): # Get the province name (section heading) province = section.css("h4.page-head-line::text").get() # Get cities under this province for li in section.css("li"): city_name = li.css("a::text").get() city_link = li.css("a::attr(href)").get() cities.append({"provice": province, "city": city_name, "link": city_link}) # Convert the results to JSON json_output = json.dumps(cities, ensure_ascii=False, indent=4) # Print JSON output print(json_output)url = 'http://mob.yinhangkahao.com/yidong/index.html' a = get_results(url)
- Assistant
Here's an enhanced version of your script that uses the
urljoinfunction from to handle relative URLs and a few other improvements for better code practice. I'll also ensure that the indentation and logical structure are correct. Here's the revised version: - User
fix code import requests from parsel import Selector from lib.dbdog import DBdog from diskcache import Cache import time import json from urllib.parse import urljoin
UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36'
dbd = DBdog() # Initialize DBdog instance
cache = Cache(directory='cache')
@cache.memoize() def fetch_url(url): response = requests.get(url, headers={'user-agent': UA}, timeout=10) response.raise_for_status()
return response.textdef get_results(url): html_content = fetch_url(url) # Use parsel to select and extract data # Parse the HTML content selector = Selector(text=html_content)
# Initialize a dictionary to hold the results cities = [] # Extract headings and corresponding cities for section in selector.css(".row.colbtm"): # Get the province name (section heading) province = section.css("h4.page-head-line::text").get() # Get cities under this province for li in section.css("li"): city_name = li.css("a::text").get() city_link = li.css("a::attr(href)").get() city_link = urljoin(url, city_link.strip()) cities.append({"provice": province, "city": city_name, "isp_name", isp_name, "link": city_link}) # Convert the results to JSON json_output = json.dumps(cities, ensure_ascii=False, indent=4) # Print JSON output print(json_output) - Assistant