区域链接(district)

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int n, color[5005], num[5005];
int main() {
	int t;
	cin >> t;
	while(t--) {
		memset(num, 0, sizeof(num));
		int tmp;
		cin >> n;
		for (int i = 1; i <= n; i++) {
			cin >> color[i];
		}
		int flag = 1;
		for (int i = 2; i <= n; i++) {
			if(color[i - 1] != color[i]) {
				flag = 0;
			}
		}
		if(flag) {
			//cout << endl;
			//for (int i = 1; i <= n; i++) cout << color[i] << ' ';
			//cout << endl;
			cout << "NO" << endl;
		} else {
			cout << "YES" << endl;
			int x = color[1];
			for (int i = 2; i <= n; i++) {
				if(color[i] != x) {
					tmp = i;
					cout << 1 << ' ' << i << endl;
					num[i] = 1;
				} 
			}
			for (int i = 2; i <= n; i++) {
				if(num[i] == 0) {
					cout << tmp << ' ' << i << endl;
				}
			}
		}
	}
	return 0;
}

运行代码后提示我页面结构变更,line56行报错,应该怎么处理, import csv from selenium import webdriver from bs4 import BeautifulSoup from urllib.parse import urljoin def getText(city, url, accumulated_text=“”): try: driver.get(url) driver.implicitly_wait(30) # 增加等待时间 soup = BeautifulSoup(driver.page_source, “lxml”) # 提取正文内容 content_div = soup.find(class_='content') if content_div: paragraphs = content_div.find_all('p') current_text = "".join([p.text.strip() for p in paragraphs]) accumulated_text += current_text else: accumulated_text += "【内容区域未找到】" # 查找下一页 next_page = soup.find('a', text='下一页') if next_page and 'href' in next_page.attrs: next_url = urljoin(url, next_page['href']) return getText(city, next_url, accumulated_text) # 递归处理下一页 # 没有下一页时保存结果 saveText(city, accumulated_text) except Exception as e: print(f"处理{city}时发生错误:{str(e)}") saveText(city, accumulated_text + "【抓取异常】") def saveText(city, text): with open(‘government_reports.csv’, ‘a’, newline=‘’, encoding=‘utf-8’) as f: csv.writer(f).writerow([city, text]) if name == ‘main’: # 初始化CSV文件 with open(‘government_reports.csv’, ‘w’, newline=‘’, encoding=‘utf-8’) as f: csv.writer(f).writerow([‘城市’, ‘报告内容’]) # 配置Edge浏览器驱动 driver = webdriver.Edge() try: # 获取目录页 driver.get("http://district.ce.cn/zt/2023/zfgzbg/index.shtml") soup = BeautifulSoup(driver.page_source, "lxml") # 验证容器元素存在 con_div = soup.find(class_='con') if not con_div: raise ValueError("页面结构已变更,未找到class='con'的容器") # 提取城市链接 city_links = [] for a_tag in con_div.find_all('a'): if not a_tag.has_attr('href') or not a_tag.has_attr('title'): continue city_links.append(( a_tag['title'], urljoin(driver.current_url, a_tag['href']) )) # 遍历所有城市页面 for city_name, link in city_links: print(f"正在处理:{city_name}") getText(city_name, link) finally: driver.quit() print("所有任务已完成,浏览器已关闭")
03-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值