python爬取网页源代码,提取关键词信息

python 复制代码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import csv
from tqdm import tqdm

# 设置Chrome选项,使其在无头模式下运行
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')
# 禁止加载图片
prefs = {"profile.managed_default_content_settings.images": 2}
chrome_options.add_experimental_option("prefs", prefs)

# CSV文件名
csv_file = 'lists_3.csv'
# 指定ChromeDriver的路径
driver_path = '/usr/bin/chromedriver'

# 创建一个Service对象
service = Service(driver_path)

# 创建WebDriver实例
driver = webdriver.Chrome(service=service, options=chrome_options)

# 从CSV文件读取URL列表
urls = []
with open(csv_file, mode='r', encoding='utf-8') as file:
    csv_reader = csv.reader(file)
    for row in csv_reader:
        if row:  # 确保行不是空的
            urls.append(row[0])  # 假设URL在每行的第一个元素

# 输出文件
output_file = 'code.txt'

# 使用'a'模式打开输出文件
with open(output_file, 'a', encoding='utf-8') as file:
    for url in tqdm(urls, desc="Processing URLs"):
        try:
            # 访问URL
            driver.get(url)
            
            # 显式等待页面标题出现
            WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "title")))
            
            # 获取页面标题
            title = driver.title
            
            # 尝试获取<title>,并写入文件
            file.write(f"{url}\n")
            file.write(f"Title: {title}\n")
            
            # 尝试获取<meta name="keywords">
            try:
                WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.NAME, "keywords")))
                keywords = driver.find_element(By.NAME, 'keywords').get_attribute('content')
                file.write(f"Keywords: {keywords}\n")
            except Exception:
                file.write("Keywords: Not Found\n")
            
            # 尝试获取<meta name="description">
            try:
                WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.NAME, "description")))
                description = driver.find_element(By.NAME, 'description').get_attribute('content')
                file.write(f"Description: {description}\n")
            except Exception:
                file.write("Description: Not Found\n")
            
            file.write("\n")  # 分隔不同URL的元数据
            file.flush()  # 强制刷新文件缓冲
            
        except Exception as e:
            print(f"An error occurred while processing {url}: {str(e)}")
            continue  # 跳过错误URL,继续处理下一个

# 关闭WebDriver实例
driver.quit()

print("All data has been written to the output file.")
相关推荐
毕设源码-朱学姐38 分钟前
【开题答辩全过程】以 基于Java的医务室病历管理小程序为例,包含答辩的问题和答案
java·开发语言·小程序
APIshop1 小时前
代码实战:PHP爬虫抓取信息及反爬虫API接口
开发语言·爬虫·php
kyle~1 小时前
C++---关键字constexpr
java·开发语言·c++
weixin_438694391 小时前
pnpm 安装依赖后 仍然启动报的问题
开发语言·前端·javascript·经验分享
咋吃都不胖lyh2 小时前
比较两个excel文件的指定列是否一致
爬虫·python·pandas
阿凡达蘑菇灯2 小时前
langgraph---条件边
开发语言·前端·javascript
Han.miracle2 小时前
Java的多线程——多线程(3)线程安全
java·开发语言·jvm·学习·安全·线程·多线程
0小豆02 小时前
【系列开篇】从零构建智能字幕校准系统:一个AI+微服务的完整实战之旅
spring boot·python·nlp·微服务架构·实战项目·spacy·ai算法
周杰伦_Jay3 小时前
【主流开发语言深度对比】Python/Go/Java/JS/Rust/C++评测
开发语言·python·golang
ldmd2843 小时前
Go语言实战:入门篇-5:函数、服务接口和Swagger UI
开发语言·后端·golang