跳转至

社交网络安全与隐私记录

约 708 个字 91 行代码 预计阅读时间 6 分钟

Intro

说明

这个是用来记录这门课做作业时候的一些知识点

凑国际化选的课,内容是与网络安全相关的,但由于不考试所以课基本没听,slides 基本没看,具体的介绍可以看图灵班的介绍

这个课正如许老师自己说的:“课的内容是很水,但是作业还是比较硬的。”这次的作业是在网上爬取 PromptBase 网站上个人卖家的信息并做数据分析,主要用到的工具是爬虫和一些数据分析的工具,爬虫没接触过但是这次的内容比较简单,数据分析有一些基础,所以作业整体不难只是工作量会有些大,毕竟要用英文写三页以上的报告,老师会给报告模板和范例。大作业做的是重定向相关,助教会给 tutorial ,虽然做的有点赶但是基本上平稳落地了哈哈哈(组内有大佬)。不过真的能学到很多很有趣的东西。

许老师人真的超nice,也很和善,虽然会点名但都是数字而且都会写在黑板上。助教人美心善超级好,我们组大作业遇到 bug 了会帮忙下载数据,而且发信息都是秒回的!

网页正则化

网址:https://promptbase.com/prompt/* 正则表达式:https://promptbase\.com/prompt/.*?(?=")

  • .*? 非贪婪匹配任意字符:. 匹配任意一个字符(除了换行),* 表示前面的 . 可以出现 0 次或多次,?* 变成非贪婪,经可能减少匹配字符(否则默认尽可能多地匹配)

  • (?=") 正向前瞻,要求后面紧跟一个英文引号" ,但不把 " 包含在最终匹配结果里,(?=...) 是一种零宽断言,只检查后面有没有特定内容,不消耗字符。

爬虫

Python + Selenium

Python
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options

# 设置 Edge WebDriver 的路径
edge_driver_path = r"C:\WebDriver\msedgedriver.exe"  # 请根据实际路径修改

# 创建 EdgeOptions 对象
edge_options = Options()
edge_options.use_chromium = True  # 使用 Chromium 内核
edge_options.add_argument("--start-maximized")  # 最大化窗口
# edge_options.add_argument("--headless")  # 无头模式(如需)

# 创建 Service 对象
service = Service(executable_path=edge_driver_path)

# 初始化 WebDriver
driver = webdriver.Edge(service=service, options=edge_options)

# 打开目标网页
driver.get("https://www.example.com")

# 等待几秒查看效果
import time
time.sleep(5)

# 关闭浏览器
driver.quit()

在实际项目中,经常需要更细致地控制,比如:

  • 指定 EdgeDriver 路径
  • 设置浏览器启动选择选项
  • 兼容新版 Selenium 设计

推荐分开声明 Service 和 Options 的方式


通常我们更喜欢用class属性、id属性、或者有意义的标签来定位,这样即使网页内部小改动,也能稳稳地找到你要的数据。

Text Only
1
2
3
4
5
6
<div class="item-stat-top">
    <span>123 Views</span>
    <span>45 Likes</span>
    <span>67 Downloads</span>
    <span>89 Sales</span>
</div>
场景 方法
定位某个容器 find_element(By.CLASS_NAME,'item-stat-top')
找这个容器下的所有 span(数据用 <span> 包裹) find_elements(By.TAG_NAME,'span')
提取文本内容 span.text

Note

find_element 默认是第一个匹配到的元素 find_elements 是抓取全部匹配的元素

HTML
1
2
3
4
5
<div class="item-stat-top">
    <div class="inner-div">
        <span>123 Views</span>
    </div>
</div>

在 Selenium 中,从子元素定位到父元素的方法:使用 .find_element(By.XPATH,"..") 其中 .. 在 XPath 中代表父节点,采用 ../.. 可以多跳一层结构。

Python
from selenium import webdriver
from selenium.webdriver.common.by import By
import json

# 启动 Selenium (以 Edge 为例)
driver = webdriver.Edge()

# 打开网页
driver.get('https://example.com')  # 替换成你的 URL

# 找到 <script> 元素
script_element = driver.find_element(By.ID, 'ng-state')

# 取出它的文本内容
json_text = script_element.get_attribute('innerHTML')

# 解析成 Python 字典
data = json.loads(json_text)

# 打印出来看看
print(data)

driver.quit()
Python
import csv

# 假设你的变量是这些(从爬虫那里得到的)
PromptName = "Food Advertising Photography"
price = 4.99
rating = 4.89
likes = 378
WordCount = 25
description = "This curated prompt generates high-quality impactful food images..."
tags = ['food', 'photograph', 'food_photograph', 'photograph_food']

# 把 tags 列表转成字符串
tags_str = ",".join(tags)

# 定义要写入的一行数据
row = [PromptName, price, rating, likes, WordCount, description, tags_str]

# 文件名
filename = 'prompts.csv'

# 写入(如果第一次写,需要写表头;之后追加)
try:
    with open(filename, 'x', newline='', encoding='utf-8') as f:
        # 第一次打开,用 'x' 模式(只在文件不存在时新建)
        writer = csv.writer(f)
        writer.writerow(['PromptName', 'Price', 'Rating', 'Likes', 'WordCount', 'Description', 'Tags'])  # 写表头
        writer.writerow(row)  # 写第一行数据
except FileExistsError:
    # 文件已存在,直接追加
    with open(filename, 'a', newline='', encoding='utf-8') as f:
        writer = csv.writer(f)
        writer.writerow(row)

Data Analysis