当前位置: 首页 > news >正文

ppt的免费网站网站做cdn

ppt的免费网站,网站做cdn,图片加文字,计算机网络 网站1.马蜂窝爬虫 编程语言:Python爬虫框架:Selenium(用于浏览器自动化)解析库:BeautifulSoup(用于解析HTML) 2.爬虫策略 目标网站:马蜂窝(https://www.mafengwo.cn/&…

1.马蜂窝爬虫

  • 编程语言:Python
  • 爬虫框架:Selenium(用于浏览器自动化)
  • 解析库:BeautifulSoup(用于解析HTML)

2.爬虫策略

  • 目标网站:马蜂窝(https://www.mafengwo.cn/)
  • 目标数据:大连的攻略和游记
  • 流程概述
    1. 打开马蜂窝首页并进行搜索。
    2. 提取搜索结果页面中的攻略和游记链接。
    3. 分别访问每个攻略和游记页面,提取内容并保存到本地文件。

3.导入的库

import randomimport timefrom bs4 import BeautifulSoupfrom selenium import webdriverfrom selenium.webdriver.chrome.service import Servicefrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECimport os

4.配置文件路径和ChromeDriver路径,下载对应Chrome浏览器版本的chromedriver.exe文件

file_path = 'D:\\Pycharm\\space\\Mafengwo\\mafengwo_01.txt'  # 保存内容的文件路径chrome_driver_path = "D:\\chromedriver\\chromedriver.exe"  # ChromeDriver路径

5.为了防止爬取的时候出现中断,采取get_links(file_path)函数从文件中读取已爬取的链接来解决中途爬取中断的问题,从而在上次中断的位置继续爬取。

def get_links(file_path):"""从文件中读取已爬取的链接"""if not os.path.exists(file_path):return set()with open(file_path, 'r', encoding='utf-8') as f:links = f.read().splitlines()return set(links)def save_link(file_path, link):"""将链接保存到文件"""with open(file_path, 'a', encoding='utf-8') as f:f.write(link + '\n')# 已爬取的链接文件already_crawled_file = 'already_crawled_01.txt'

6.初始化Selenium WebDriver

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)

7.打开马蜂窝网站并搜索“大连”,crawl_category()函数获取攻略和游记的链接,crawl_content()函数爬取指定链接的内容,crawl_content()设计了防止网页内容爬取不完整的情况出现和反爬虫机制的情况出现,采用滚轮的方式处理,滚动次数和等待页面加载时间可以根据实际情况来进行调整,最后获得爬取攻略和游记内容。

"""4. 打开马蜂窝网站并搜索“大连”"""driver.get("https://www.mafengwo.cn/")driver.maximize_window()wait = WebDriverWait(driver, 10)wait.until(EC.visibility_of_element_located((By.ID, '_j_index_search_input_all')))start_input = driver.find_element(By.ID, '_j_index_search_input_all')start_input.send_keys('大连')search_button = driver.find_element(By.ID, '_j_index_search_btn_all')search_button.click()time.sleep(random.randint(0, 3))"""5. 获取攻略和游记的链接"""def crawl_category(category_link):"""爬取指定分类下的攻略或游记链接"""driver.get(category_link)wait = WebDriverWait(driver, 10)wait.until(EC.visibility_of_element_located((By.ID, '_j_search_result_left')))outer_div = driver.find_element(By.ID, '_j_search_result_left')a_elements = outer_div.find_elements(By.XPATH, './/div[@class="flt1"]/a')hrefs = [a_element.get_attribute('href') for a_element in a_elements]already_crawled = get_links(already_crawled_file)for href in hrefs:if href not in already_crawled:save_link(already_crawled_file, href)crawl_content(href)def crawl_content(link):"""爬取指定链接的内容"""driver.get(link)wait = WebDriverWait(driver, 10)# 滚动页面加载更多内容for _ in range(7):  # 滚动次数,根据实际情况调整driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")time.sleep(random.randint(1, 3))  # 等待页面加载if "gl" in link:wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'sideL')))soup = BeautifulSoup(driver.page_source, 'html.parser')content_box = soup.find('div', class_='sideL')else:wait.until(EC.visibility_of_element_located((By.CLASS_NAME, '_j_content_box')))soup = BeautifulSoup(driver.page_source, 'html.parser')content_box = soup.find(class_="_j_content_box")all_text = ' '.join(content_box.stripped_strings)with open(file_path, 'a', encoding='utf-8') as file:file.write(all_text + '\n')time.sleep(random.randint(0, 3))# 获取攻略和游记的链接element = driver.find_element(By.XPATH, '//*[@id="_j_mfw_search_main"]/div[1]/div/div/a[3]')notes = element.get_attribute('href')element = driver.find_element(By.XPATH, '//*[@id="_j_mfw_search_main"]/div[1]/div/div/a[4]')guides = element.get_attribute('href')# 爬取攻略和游记内容crawl_category(guides)crawl_category(notes)driver.quit()

8.结果展示:

http://www.yayakq.cn/news/661096/

相关文章:

  • django 网站开发案例ios开发教程
  • wordpress 网站标题设置wordpress精美主题
  • 网站开发实战答案大众点评seo关键词优化
  • 行业门户网站运营方案广东省广州市番禺区
  • 网站视觉分析聊城网站建设优化
  • 外贸网站在哪做外链淄博亿泰
  • seo网站推广主要目的不包括wordpress 如何改中文
  • 网站建站报价单做淘宝客网站性质
  • 男男做暧网站免费wordpress教程 好看
  • 电商供货平台唐山seo公司
  • 个人空间网站建设wordpress用户投稿
  • 外国人做那个视频网站吗利于优化的网站模板
  • wordpress滑动插件兰州企业网络优化服务
  • ps做网站主页的效果图有趣的网站代码
  • 自助免费网站制作全国做网站的大公司有哪些
  • 网站优化一年多少钱泉州城乡建设网站
  • 外贸建站与推广如何做 googleandroid网站开发视频教程
  • 要求维护公司做网站整改的函万州论坛网站建设
  • 北京网站建设报价明细微分销平台登陆
  • 房屋在线设计网站建设网站需要哪些域名
  • 有没有国外的做美食的视频网站网站制作1
  • 加强门户网站建设方案企业网站建设收费
  • 关于做ppt的网站有哪些内容吗建设通网站是什么时间成立
  • 如何建立公司企业网站网站开发公司郑州
  • 网站开发的体会注册网站英语怎么说
  • 好的网站推荐局门户网站的建设
  • 网站模版如何建qq官方网页版登录
  • 上海网站开发团队广州市省建设厅网站
  • 建站哪家好佛山宣传片制作
  • 石林彝族网站建设营销型网站建设哪里济南兴田德润优惠吗