标签: code

  • 【网站】WordPress使用Google Analytics代码的简单方法

    写在前面:谷歌分析会被浏览器的反广告和反追踪插件屏蔽,因此搜集到的数据可能有很大缺失。

    方法一:通过页眉+自定义HTML实现

    一般的网站页眉部分是统一的,如果不是的话,可以设置一个统一的空白的页眉,只需要将Google Analytics提供的script代码以HTML区块的方式粘贴到页眉部分即可全站启用,并为全站提供分析代码。

    方法二:通过模板函数实现(更推荐)

    使用类似的代码,插入到主题模板函数中,这样可以直接在Header中进行加载。

    // Function to add Google Analytics
    function add_google_analytics() {
        ?>
    <!-- Google tag (gtag.js) -->
        <script async src="https://www.googletagmanager.com/gtag/js?id=GA_TRACKING_ID"></script> // 替换你的ID
        <script>
          window.dataLayer = window.dataLayer || [];
          function gtag(){dataLayer.push(arguments);}
          gtag('js', new Date());
          gtag('config', 'GA_TRACKING_ID'); // 替换你的ID
        </script>
        <!-- End Google Analytics -->
        <?php
    }
    add_action('wp_head', 'add_google_analytics');

  • 【网站】为WordPress添加APlayer播放器

    引入方式类似prism,由于使用概率较大,因此在主题模板函数中进行全局引入:

    // Function to add APlayer.mini.css and APlayer.mini.js to the site
    function add_APlayer() {
        
        wp_register_style(
            'APlayerCSS', // handle name for the style 
            get_stylesheet_directory_uri() . '/APlayer.css' // location of the file
        );
    
        wp_register_script(
            'APlayerJS', // handle name for the script 
            get_stylesheet_directory_uri() . '/APlayer.js' // location of the file
        );
    
        // Enqueue the registered style and script files
        wp_enqueue_style('APlayerCSS');
        wp_enqueue_script('APlayerJS');
    }
    add_action('wp_enqueue_scripts', 'add_APlayer');

    然后采用 APlayer官方文档 中描述的方式引入:

    <div id="aplayer">
    </div>
    <script>
    const ap = new APlayer({
        container: document.getElementById('aplayer'),
        audio: [{
            name: '',
            artist: '',
            url: '',
            cover: ''
        }]
    });
    </script>

  • 【Python】对本地网页进行元素提取并输出Excel

    一些网页通过加载Js来保护页面元素,当我们突破Js得到本地页面时,可以使用BS4库对页面进行分析,提取对应的元素来综合有价值的内容。

    示例代码:

    import requests
    from bs4 import BeautifulSoup
    import pandas as pd
    
    # 发送请求并获取网页内容
    url = 'your_local_or_online_page_url'
    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'html.parser')
    
    # 定义一个空的列表来存储提取的数据
    data = []
    
    # 遍历页面中的项目列表,假设项目数据都在某个父元素中
    projects = soup.find_all('tr', class_='project-id')  # 根据实际情况修改选择器
    
    # 提取每个项目的各项数据
    for project in projects:
        # 获取项目ID
        project_id = project.find('td', class_='project-id-class').get_text(strip=True)  # 修改为实际选择器
        
        # 将提取的数据添加到列表中
        data.append([project_id]) # 按实际修改
    
    # 创建 DataFrame 并保存为 Excel
    df = pd.DataFrame(data, columns=['ID']) # 按实际修改
    df.to_excel('projects_data.xlsx', index=False)
    
    print("Data has been successfully extracted and saved to 'projects_data.xlsx'.")

    主要用到了BS4库。

    示意代码:

    from bs4 import BeautifulSoup
    
    # 假设有一个HTML文档
    html_doc = """
    <html>
      <head><title>Example Page</title></head>
      <body>
        <p class="title"><b>Sample Page</b></p>
        <p class="story">This is a test story. <a href="http://example.com/1" class="link">link1</a> <a href="http://example.com/2" class="link">link2</a></p>
        <p class="story">Another test story.</p>
      </body>
    </html>
    """
    
    # 使用 BeautifulSoup 解析 HTML 文档
    soup = BeautifulSoup(html_doc, 'html.parser')
    
    # 提取<title>标签的内容
    title = soup.title.string
    print(f"Title: {title}")
    
    # 提取所有的链接(<a> 标签)
    links = soup.find_all('a')
    for link in links:
        print(f"Link text: {link.string}, URL: {link['href']}")
    
    # 查找特定类的<p>标签
    story_paragraphs = soup.find_all('p', class_='story')
    for p in story_paragraphs:
        print(f"Story paragraph: {p.get_text()}")