与风景对话_交互式旅游推荐系统_数据收集与预处理_04

2. 爬虫设计

爬虫设计是数据收集过程中至关重要的环节,直接影响到数据的质量和数量。下面详细介绍爬虫设计的各个方面,包括爬虫工具选择、爬虫策略、反爬机制应对、数据提取与存储、数据清洗等内容。通过精心设计和实施爬虫,我们将确保从去哪儿网、马蜂窝和小红书等平台高效获取高质量的旅游数据。

2.3 小红书爬虫

XiaoHongShuSpider.py是一个用于爬取小红书网站上有关“东北旅游”相关信息的爬虫。代码通过Selenium控制浏览器模拟用户行为,以获取动态加载的数据,并使用BeautifulSoup解析页面内容。下面是对代码的详细分析:

2.3.1 初始化
result_list = []

chrome_driver_path = 'D:\\App\\chromedriver\\chromedriver.exe'  # 替换为你的ChromeDriver路径
s = Service(chrome_driver_path)
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_argument("--disable-blink-features=AutomationControlled")
driver = webdriver.Chrome(service=s, options=options)
  1. result_list = []:初始化一个空列表,用于存储爬取的结果。
  2. chrome_driver_path:设置ChromeDriver的路径。
  3. s = Service(chrome_driver_path):创建ChromeDriver服务。
  4. options = webdriver.ChromeOptions():初始化Chrome浏览器的选项。
  5. options.add_experimental_option('excludeSwitches', ['enable-automation'])options.add_argument("--disable-blink-features=AutomationControlled"):配置浏览器以防止被检测到是自动化控制。
  6. driver = webdriver.Chrome(service=s, options=options):启动Chrome浏览器。
2.3.2 数据处理函数
chinese_months = {
    1: '一月',
    2: '二月',
    3: '三月',
    4: '四月',
    5: '五月',
    6: '六月',
    7: '七月',
    8: '八月',
    9: '九月',
    10: '十月',
    11: '十一月',
    12: '十二月'
}
  1. chinese_months:定义一个字典,将数字月份转换为中文月份。
def scroll_page(driver, distance):
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight - " + str(distance) + ");")
    time.sleep(1)  # 等待页面加载新的内容
  1. scroll_page(driver, distance):定义一个函数,通过JavaScript滚动页面一定距离,并等待1秒。
def remove_after_comment(text):
    index = text.find("【备注】")
    if index != -1:
        return text[:index]
    else:
        return text
  1. remove_after_comment(text):定义一个函数,去除文本中“备注”之后的内容。
def get_season(month):
    if 3 <= month <= 5:
        return '春天'
    elif 6 <= month <= 8:
        return '夏天'
    elif 9 <= month <= 11:
        return '秋天'
    else:
        return '冬天'
  1. get_season(month):定义一个函数,根据月份确定季节。
def change(text):
    if '/' in text:
        try:
            date_obj = datetime.strptime(text, '%Y/%m/%d')
            month_name = chinese_months[date_obj.month]
            season = get_season(date_obj.month)
            result_list.append(f'{month_name}')
            result_list.append(f'{season}')
        except ValueError:
            pass
    elif text.isdigit():
        if int(text) < 99:
            result_list.append(f'{text}天')
        else:
            result_list.append(f'{text}元')
    else:
        if text not in result_list:
            result_list.append(text)
  1. change(text):定义一个函数,根据输入文本格式,处理日期和数值并添加到result_list
2.3.3 获取文章链接
driver.get('https://www.xiaohongshu.com/search_result?keyword=%25E4%25B8%259C%25E5%258C%2597%25E6%2597%2585%25E6%25B8%25B8&source=web_explore_feed')
time.sleep(5)  # 等待页面加载
  1. driver.get(...):访问指定的搜索结果页面。
  2. time.sleep(5):等待页面加载完成。
for _ in range(10):  # 根据需要调整范围,确保加载更多内容
    scroll_page(driver, 1000)
  1. 使用scroll_page函数滚动页面以加载更多内容。
soup = BeautifulSoup(driver.page_source, 'html.parser')
article_links = []
for a_tag in soup.find_all('a', href=True):
    href = a_tag['href']
    if '/explore/' in href:
        article_links.append('https://www.xiaohongshu.com' + href)
  1. soup = BeautifulSoup(driver.page_source, 'html.parser'):使用BeautifulSoup解析页面内容。
  2. 遍历所有a标签,提取带有/explore/的链接并存储在article_links列表中。
with open('小红书东北链接.txt', 'w', encoding='utf-8') as file:
    for link in article_links:
        file.write(link + '\n')
  1. 将所有文章链接写入小红书东北链接.txt文件。
2.3.4 获取文章内容
with open('小红书东北链接.txt', 'r') as file:
    for line in file:
        result_list = []
        line = line.strip()
        last_part = line.rsplit('/', 1)[-1]
        time.sleep(1)
        driver.get(line)
        driver.maximize_window()
  1. 读取之前保存的链接文件,逐行处理每个链接。
  2. 访问每个链接并最大化浏览器窗口。
        try:
            wait = WebDriverWait(driver, 10)
            wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'note-content')))
        except TimeoutException:
            continue
  1. 使用显式等待,直到note-content元素可见。
        soup = BeautifulSoup(driver.page_source, 'html.parser')
        content_div = soup.find('div', {'class': 'note-content'})
        if not content_div:
            continue
        text_content = content_div.get_text(strip=True, separator='\n')
  1. 使用BeautifulSoup解析文章内容,获取note-content中的文本。
        data = {
            'instruction': '',
            'summary': '',
            'output': remove_after_comment(text_content)
        }

        with open('小红书东北.json', 'a', encoding='utf-8') as file:
            json.dump(data, file, ensure_ascii=False, indent=4)
            file.write('\n')
  1. 构建数据字典并将其以JSON格式追加到小红书东北.json文件。
time.sleep(3)
driver.quit()
  1. 爬取完成后,等待3秒并关闭浏览器。

这个爬虫脚本通过模拟用户行为滚动页面并抓取动态加载的数据,然后提取文章内容并保存为JSON文件。

2.3.5 数据展示

爬取的链接文件:

在3.中通过获取旅游攻略的链接,得到数据文本文件,保存到了小红书东北链接.txt

在这里插入图片描述
在这里插入图片描述

爬取的数据:

通过上面爬取的小红书东北链接.txt,访问链接通过4.爬取到真正的数据文件并保存为JSON文件

在这里插入图片描述

在这里插入图片描述

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值