# Easy Crawler
该Python脚本仅用于爬取页面文本信息,作为语言训练的数据集所使用
本来是个人使用的,只用来爬贴吧,结果发现还能爬其他网页,于是干脆修改了一下。
## 简介
## Easy Crawler 简易网络爬虫 v0.1
基本上可以爬取大部分页面文本信息。
## 优点
1. 在爬取后可进行去重、删除带有关键字词的文本。
2. 爬取配置操作简易。
3. 可绕过(至少能绕过百度的)身份验证环节。
## 缺陷
1. 爬取的结果乱序。
2. 部分页面爬取的内容无法换行。
3. 一次仅能抓取一类标签的文本。
## 使用方法
### 0. 安装requirements内的所需库(如果需要的话)
### 1. 运行Crawler.py
### 2. 根据提示填入爬取的网址、页数
### 3. 为填入剩下内容,则需要在你想要爬取的网页上按下F12,这里以火狐浏览器为例:
使用“选取页面中的元素”工具,查看你想爬取内容的一部分的源码。
找到类似 div id = "A" class="B" 的内容,其中id、class是标签,"A""B"是标签的具体名
### 4. 根据剩下提示继续进行。
### 5. 在根目录下找到"Output_(X).txt",即为输出文件。
## Q&A
Q: 爬取后的输出为空文本。
A: 可能被身份验证环节拦截了,可以在Chrome加载页面后的代码加入一个input()之类的,完成身份验证后重新运行。当然也有可能标签填写错误,导致找不到要爬取的内容。
Q: 有的标签具体名的结尾加了空格,我也需要加上么?
A: 我自己的试的时候是不用加的。
# 源码
Crawler.py
import os
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from DelKeyWord import remove_keyword, remove_multi
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/112.0'
}
webdriver = webdriver.Chrome()
# 首先我们写好抓取网页的函数
def get_html(url):
try:
r = requests.get(
url,
headers=headers
)
r.raise_for_status()
# r.endcodding = r.apparent_endconding
r.encoding = 'utf-8'
return r.text
except:
return " ERROR "
def get_content(url, cli_type, cli_class):
try:
comments = []
webdriver.get(url)
html = webdriver.page_source
soup = BeautifulSoup(html, 'lxml')
# liTags = soup.find_all('div', attrs={'class': 'd_post_content j_d_post_content'})
liTags = soup.find_all('div', attrs={cli_type: cli_class})
except:
return " ERROR "
# 通过循环找到每个帖子里的我们需要的信息:
for li in liTags:
comment = {}
if li.text.strip():
comment['content'] = li.text.strip()+"\n"
comments.append(comment)
return comments
def Out2File(dicts):
with open(file_name, 'a+', encoding='utf-8') as f:
for comment in dicts:
if (not comment) or (('点击展开,查看完整图片' or '该楼层疑似违规已被系统折叠,隐藏此楼查看此楼') in dicts):
continue
f.write('{}'.format(comment['content']))
print('当前页面爬取完成')
def main(base_url, deep, cli_type, cli_class):
# 将所有需要爬去的url存入列表
if deep:
url_list = []
for i in range(1, int(deep)):
url_list.append(base_url + '?pn=' + str(i))
print('所有的网页已经下载到本地! 开始筛选信息...')
else:
url_list = [base_url]
print('所有的网页已经下载到本地! 开始筛选信息...')
# 循环写入所有的数据
for url in url_list:
content = get_content(url, cli_type, cli_class)
Out2File(content)
print('所有的信息都已经保存完毕!正在进行数据集规范化...')
if __name__ == '__main__':
while True:
base_url = input('请输入需要爬取的贴吧链接:')
deep = input('请输入需要爬取的页数:(输入0则仅爬取单页)\n目前多页数爬取仅支持百度贴吧!')
cli_type = input('请输入需要爬取的类(class、id等):\n若填0则爬取页面所有内容.')
cli_class = input('请输入需要爬取的标识(若无填0):')
r_m = input('是否需要删除重复行(需要填1,反之填0):')
r_k = input('填写需要删除的关键字(没有请填0):')
idx = 1
while True:
if not os.path.exists('Output_' + str(idx) + '.txt'):
file_name = 'Output_' + str(idx) + '.txt'
break
idx += 1
main(base_url, deep, cli_type, cli_class)
if r_m:
remove_multi(file_name)
if not r_k:
remove_keyword(file_name, r_k)
print('已将输出规范为数据集格式.')
if not int(input('是否继续爬取?(输入0结束)')):
break
DelKeyWord.py
def remove_keyword(filepath, keyword):
"""逐行检查文件并删除包含指定关键字的行"""
with open(filepath, 'r', encoding='utf-8') as f:
lines = f.readlines()
with open(filepath, 'w', encoding='utf-8') as f:
for line in lines:
if keyword not in line:
f.write(line)
def remove_multi(file_path):
# 读取文件内容到列表
with open(file_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
# 使用set去重
lines_set = set(lines)
# 将去重后的内容写回文件
with open(file_path, 'w', encoding='utf-8') as f:
f.writelines(lines_set)
print(f'{len(lines) - len(lines_set)} 个重复行被删除')