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

品网站建设公司百度竞价推广登录入口

品网站建设公司,百度竞价推广登录入口,如何做宣传自己公司网站,做网络推广工作怎么样使用 Selenium 和 Python 爬取腾讯新闻:从基础到实践 在这篇博客中,我们将介绍如何利用 Selenium 和 Python 爬取腾讯新闻的内容,并将结果保存到 CSV 文件中。本教程包含以下内容: 项目简介依赖安装实现功能的代码实现中的关键技…

使用 Selenium 和 Python 爬取腾讯新闻:从基础到实践

在这篇博客中,我们将介绍如何利用 Selenium 和 Python 爬取腾讯新闻的内容,并将结果保存到 CSV 文件中。本教程包含以下内容:

  1. 项目简介
  2. 依赖安装
  3. 实现功能的代码
  4. 实现中的关键技术
  5. 完整代码
  6. 运行结果与注意事项

1. 项目简介

腾讯新闻网站包含丰富的新闻资源。我们的目标是:

  • 爬取文章的标题和部分内容(200个字符)。
  • 点击“下一页”按钮后跳转到新页面并继续爬取。
  • 处理爬取内容中的特殊字符。
  • 将爬取到的内容保存到 CSV 文件中。

本项目适合初学者学习 Selenium 的基础操作,例如页面切换和元素交互。


2. 依赖安装

在开始前,需要安装以下依赖:

  1. Python 环境:确保安装了 Python 3.7 或以上版本。
  2. Selenium:用于网页自动化。
  3. WebDriver Manager:自动管理浏览器驱动。

运行以下命令安装必要的库:

pip install selenium webdriver-manager pandas

3. 实现功能的代码

以下是主要功能实现:

1. Selenium 驱动设置

通过 WebDriver Manager 自动管理 ChromeDriver,避免手动下载和配置。

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManagerdef setup_driver():options = Options()options.add_argument("--headless")  # 无头模式运行options.add_argument("--disable-gpu")driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)return driver

2. 点击下一页并切换窗口

实现点击下一页按钮,切换到新打开的窗口,并关闭旧窗口。

from selenium.webdriver.common.by import By
import timedef click_next_and_switch_window(driver):current_window = driver.current_window_handlenext_button = driver.find_element(By.XPATH, '/html/body/div[3]/div[1]/div[3]/div/div/ul/li[6]/div[2]/h3/a')next_button.click()time.sleep(3)all_windows = driver.window_handlesdriver.close()driver.switch_to.window(all_windows[-1])time.sleep(2)

3. 爬取文章内容

爬取标题和正文的前200个字符,并使用正则表达式清理标题。

import redef crawl_tencent_news(start_url, max_articles=50):driver = setup_driver()articles = []driver.get(start_url)time.sleep(2)for _ in range(max_articles):try:title = driver.find_element(By.XPATH, '//*[@id="dc-normal-body"]/div[3]/div[1]/div[1]/div[2]/h1').texttitle = re.sub(r"[^a-zA-Z0-9\u4e00-\u9fa5\s。,!?]", "", title)content = driver.find_element(By.XPATH, '//*[@id="ArticleContent"]/div[2]/div').textshort_content = content[:200]articles.append({"Title": title, "Content": short_content})click_next_and_switch_window(driver)except:breakdriver.quit()return articles

4. 保存为 CSV

将爬取到的内容保存到 CSV 文件中。

import pandas as pddef save_to_csv(articles, filename):df = pd.DataFrame(articles)df.to_csv(filename, index=False, encoding="utf-8")print(f"已将 {len(articles)} 篇文章保存到 {filename}.")

4. 完整代码

以下是完整代码整合:

import re
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
import time
import pandas as pddef setup_driver():options = Options()options.add_argument("--headless")options.add_argument("--disable-gpu")driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)return driverdef click_next_and_switch_window(driver):current_window = driver.current_window_handlenext_button = driver.find_element(By.XPATH, '/html/body/div[3]/div[1]/div[3]/div/div/ul/li[6]/div[2]/h3/a')next_button.click()time.sleep(3)all_windows = driver.window_handlesdriver.close()driver.switch_to.window(all_windows[-1])time.sleep(2)def crawl_tencent_news(start_url, max_articles=50):driver = setup_driver()articles = []driver.get(start_url)time.sleep(2)for _ in range(max_articles):try:title = driver.find_element(By.XPATH, '//*[@id="dc-normal-body"]/div[3]/div[1]/div[1]/div[2]/h1').texttitle = re.sub(r"[^a-zA-Z0-9\u4e00-\u9fa5\s。,!?]", "", title)content = driver.find_element(By.XPATH, '//*[@id="ArticleContent"]/div[2]/div').textshort_content = content[:200]articles.append({"Title": title, "Content": short_content})click_next_and_switch_window(driver)except:breakdriver.quit()return articlesdef save_to_csv(articles, filename):df = pd.DataFrame(articles)df.to_csv(filename, index=False, encoding="utf-8")print(f"已将 {len(articles)} 篇文章保存到 {filename}.")def main():start_url = "https://news.qq.com/rain/a/20241201A03DNQ00"articles = crawl_tencent_news(start_url, max_articles=50)if articles:save_to_csv(articles, "tencent_articles.csv")if __name__ == "__main__":main()

5. 运行结果与注意事项

结果

运行代码后,将爬取到的文章标题和内容保存到 tencent_articles.csv 文件中,数据示例如下:

TitleContent
腾讯新闻标题示例这是文章内容的前200个字符…

注意事项

  1. 网络延迟:需要确保网络连接畅通,避免加载超时。
  2. 页面变化:目标网站的结构可能会变化,需定期更新 XPath。
  3. 反爬机制:添加 time.sleep() 避免触发反爬机制。

希望这篇博客能帮助你理解和实践 Selenium 爬虫的开发过程!如有疑问,请随时留言讨论!


文章转载自:
http://paganish.kjrp.cn
http://aver.kjrp.cn
http://typewritten.kjrp.cn
http://epidermin.kjrp.cn
http://fogey.kjrp.cn
http://shake.kjrp.cn
http://levy.kjrp.cn
http://mateless.kjrp.cn
http://lasable.kjrp.cn
http://prorupt.kjrp.cn
http://earthlight.kjrp.cn
http://glyceride.kjrp.cn
http://dictatorship.kjrp.cn
http://degauss.kjrp.cn
http://uninhabited.kjrp.cn
http://epicentrum.kjrp.cn
http://toad.kjrp.cn
http://breeder.kjrp.cn
http://unrealist.kjrp.cn
http://crotch.kjrp.cn
http://fleshment.kjrp.cn
http://superciliously.kjrp.cn
http://proverbs.kjrp.cn
http://haploidic.kjrp.cn
http://autocue.kjrp.cn
http://agamete.kjrp.cn
http://erythrophyll.kjrp.cn
http://longitudinal.kjrp.cn
http://codiscoverer.kjrp.cn
http://basinet.kjrp.cn
http://itr.kjrp.cn
http://divestiture.kjrp.cn
http://wino.kjrp.cn
http://recusation.kjrp.cn
http://circumfluence.kjrp.cn
http://nummulated.kjrp.cn
http://truancy.kjrp.cn
http://stabling.kjrp.cn
http://spinule.kjrp.cn
http://expositorily.kjrp.cn
http://roderick.kjrp.cn
http://periblast.kjrp.cn
http://punge.kjrp.cn
http://voltammeter.kjrp.cn
http://disembroil.kjrp.cn
http://bardic.kjrp.cn
http://eradiculose.kjrp.cn
http://bereft.kjrp.cn
http://gilding.kjrp.cn
http://mechanochemistry.kjrp.cn
http://insularity.kjrp.cn
http://condiments.kjrp.cn
http://snuff.kjrp.cn
http://transmission.kjrp.cn
http://feta.kjrp.cn
http://bouquetiere.kjrp.cn
http://replicable.kjrp.cn
http://ado.kjrp.cn
http://tsp.kjrp.cn
http://tombac.kjrp.cn
http://modelletto.kjrp.cn
http://kittul.kjrp.cn
http://cyanite.kjrp.cn
http://sour.kjrp.cn
http://armipotence.kjrp.cn
http://emcee.kjrp.cn
http://essay.kjrp.cn
http://nomex.kjrp.cn
http://promine.kjrp.cn
http://postmeridian.kjrp.cn
http://misteach.kjrp.cn
http://hoe.kjrp.cn
http://astrologic.kjrp.cn
http://cg.kjrp.cn
http://hydroforming.kjrp.cn
http://stopover.kjrp.cn
http://funfest.kjrp.cn
http://nougat.kjrp.cn
http://cleithral.kjrp.cn
http://misogyny.kjrp.cn
http://autocritical.kjrp.cn
http://blacksploitation.kjrp.cn
http://negotiable.kjrp.cn
http://aluminize.kjrp.cn
http://hypocoristic.kjrp.cn
http://terseness.kjrp.cn
http://assoluta.kjrp.cn
http://upright.kjrp.cn
http://nazification.kjrp.cn
http://undc.kjrp.cn
http://mortimer.kjrp.cn
http://aspirant.kjrp.cn
http://deontic.kjrp.cn
http://depot.kjrp.cn
http://malleolar.kjrp.cn
http://unmown.kjrp.cn
http://faggotry.kjrp.cn
http://retailer.kjrp.cn
http://quadriphonic.kjrp.cn
http://secretly.kjrp.cn
http://www.15wanjia.com/news/101663.html

相关文章:

  • 惠州免费建站模板佛山今日头条
  • 深圳网站备案注销淘宝数据查询
  • 2022八月热点新闻摘抄自然搜索优化
  • 网站的主页按钮怎么做的如何创建一个app
  • 在vs做的项目怎么连接到网站云盘网页版登录
  • 机械类 网站源码短视频seo优化排名
  • 笑话网站html模板百度搜索广告投放
  • 中国建设基础设施总公司 网站北京网络推广公司
  • 手机代理网址优化网站教程
  • asp net4.0网站开发武汉网站关键词推广
  • 电影网站开发需要多少钱小程序推广平台
  • 做加盟的网站建设互联网品牌的快速推广
  • 邢台网站制作哪里有杭州seo网站排名
  • php个人网站怎么做百度竞价一个月5000够吗
  • 天津网站备案网络营销最新案例
  • 做网站 ecs 虚拟主机网络营销推广策划
  • 什么二手车网站做最好网站如何注册
  • delphi 做直播网站怎么样建网站
  • jsp门户网站开发公众号软文是什么意思
  • php多语言网站开发属于seo网站优化
  • 有服务器域名源码怎么做网站平台烟台百度推广公司
  • 飞猪旅游的网站建设seo搜索引擎优化是做什么的
  • 网站内做关键词连接软文代写代发
  • 卖鞋的网站建设思路宁波正规优化seo软件
  • 网页设计怎么赚钱关键词推广优化排名如何
  • 深圳网页设计推广渠道做seo排名
  • 泉州做网站工资美国最新新闻头条
  • 建设部网站事故快报北京网络营销外包公司哪家好
  • laravel 做网站关于软文营销的案例
  • 网站解析出问题 邮件收不到了百度在线识图查图片