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

北京网站建设公司服务哪家好google搜索引擎入口2022

北京网站建设公司服务哪家好,google搜索引擎入口2022,slider wordpress,纯静态网站怎么做咱们这个任务啊#xff0c;就是要从一个指定的网站上#xff0c;抓取新闻内容#xff0c;然后把它们整整齐齐地保存到本地。具体来说#xff0c;就是要去光明网的板块里#xff0c;瞅瞅里面的新闻#xff0c;把它们一条条地保存下来。 首先#xff0c;咱得有个网址就是要从一个指定的网站上抓取新闻内容然后把它们整整齐齐地保存到本地。具体来说就是要去光明网的板块里瞅瞅里面的新闻把它们一条条地保存下来。 首先咱得有个网址这就是咱要去的地方。然后用requests这个神奇的小工具向这个网址发送个GET请求就像是对网站说“喂把你的内容给我送过来”。 接下来用lxml这个库来解析网页就像是拿到一本书咱得知道目录在哪儿正文在哪儿才能把需要的内容找出来。 咱们的目标是抓取页面上的新闻链接这些链接被放在了一系列的ul和li标签里。所以咱得一个个ul去看每个ul里面又是一堆li每个li里面才是咱们要的新闻链接。 找到链接后咱再次用requests去访问这个链接把新闻的详细内容给抓回来。标题、正文咱都要然后把它们整理一下每条新闻保存成一个txt文件文件名就按照咱抓取的顺序来编号这样方便管理。 过程中咱得注意网页上的链接可能有的是完整的有的可能就给了个后缀咱得处理好这个确保能正确访问到新闻的详细页面。然后就是把新闻的标题和内容提取出来去掉多余的空白字符整整齐齐地写入到文件里。 这样一来只要运行这段代码咱就能自动化地把网站上的新闻一条条地保存到本地了省时省力还能随时回头看看收集到的新闻呢。 后续如果需要额外的处理和补充可以私信联系我 import requests from lxml import html import os# 目标网站的url base_url https://politics.gmw.cn/ url base_url node_9844.htm# 使用requests库发送GET请求到目标网站 response requests.get(url) response.encoding utf-8 # 尝试使用utf-8解码# 解析HTML内容 tree html.fromstring(response.text) # 使用text代替content# 文件编号 file_num 1# 循环处理从ul[1]到ul[10] for ul_index in range(1, 11):# 循环处理每个ul中的li标签从li[1]开始如果没有找到li标签就跳出循环li_index 1while True:try:# 构建XPathxpath f/html/body/div[6]/div[1]/div[2]/ul[{ul_index}]/li[{li_index}]/a# 使用XPath查找特定的a标签a_tag tree.xpath(xpath)# 如果找到了a标签if a_tag:# 获取a标签的href属性也就是URLsub_url a_tag[0].get(href)sub_url base_url sub_url if not sub_url.startswith(http) else sub_urlprint(子url为,sub_url)# 获取子页面内容sub_response requests.get(sub_url)sub_response.encoding utf-8 # 尝试使用utf-8解码sub_tree html.fromstring(sub_response.text) # 使用text代替content# 获取标题title sub_tree.xpath(/html/body/div[6]/div[1]/h1/text())title title[0].strip() if title else # 去除两端的空白字符# 获取正文contents sub_tree.xpath(//*[idarticle_inbox]/div[5]/p/text())contents \n.join([content.strip() for content in contents if content.strip()]) if contents else # 去除两端的空白字符并删除空行# 写入到文件with open(f./txt/{str(file_num).zfill(2)}.txt, w, encodingutf-8, errorsignore) as f:f.write(title \n\n contents)# 更新文件编号file_num 1else:# 如果没有找到a标签就跳出循环break# 处理下一个li标签li_index 1except Exception as e:print(f处理XPath {xpath} 时发生错误: {e})break输出结果如下 子url为 https://politics.gmw.cn/2023-06/28/content_36660331.htm 子url为 https://politics.gmw.cn/2023-06/28/content_36660279.htm 子url为 https://politics.gmw.cn/2023-06/28/content_36660246.htm 子url为 https://politics.gmw.cn/2023-06/28/content_36660217.htm 子url为 https://politics.gmw.cn/2023-06/28/content_36660215.htm 子url为 https://politics.gmw.cn/2023-06/28/content_36660103.htm 子url为 https://politics.gmw.cn/2023-06/28/content_36659630.htm 子url为 https://politics.gmw.cn/2023-06/28/content_36659390.htm 子url为 https://politics.gmw.cn/2023-06/28/content_36659337.htm 子url为 https://politics.gmw.cn/2023-06/28/content_36659325.htm 子url为 https://politics.gmw.cn/2023-06/28/content_36659297.htm 子url为 https://politics.gmw.cn/2023-06/28/content_36659135.htm 子url为 https://politics.gmw.cn/2023-06/28/content_36658702.htm 子url为 https://politics.gmw.cn/2023-06/28/content_36658613.htm 子url为 https://politics.gmw.cn/2023-06/28/content_36658674.htm 子url为 https://politics.gmw.cn/2023-06/28/content_36658631.htm 子url为 https://politics.gmw.cn/2023-06/28/content_36658595.htm 子url为 https://politics.gmw.cn/2023-06/28/content_36658527.htm 子url为 https://politics.gmw.cn/2023-06/28/content_36658463.htm 子url为 https://politics.gmw.cn/2023-06/28/content_36658416.htm 子url为 https://politics.gmw.cn/2023-06/28/content_36658377.htm 子url为 https://politics.gmw.cn/2023-06/28/content_36658411.htm 子url为 https://politics.gmw.cn/2023-06/28/content_36658401.htm 子url为 https://politics.gmw.cn/2023-06/28/content_36658372.htm 子url为 https://politics.gmw.cn/2023-06/28/content_36658356.htm 子url为 https://politics.gmw.cn/2023-06/27/content_36657735.htm 子url为 https://politics.gmw.cn/2023-06/27/content_36657732.htm 子url为 https://politics.gmw.cn/2023-06/27/content_36657622.htm 子url为 https://politics.gmw.cn/2023-06/27/content_36657620.htm 子url为 https://politics.gmw.cn/2023-06/27/content_36657627.htm 子url为 https://politics.gmw.cn/2023-06/28/content_36658305.htm 子url为 https://politics.gmw.cn/2023-06/27/content_36657625.htm 子url为 https://politics.gmw.cn/2023-06/28/content_36658293.htm 子url为 https://politics.gmw.cn/2023-06/27/content_36657544.htm 子url为 https://politics.gmw.cn/2023-06/27/content_36657204.htm 子url为 https://politics.gmw.cn/2023-06/27/content_36657203.htm 子url为 https://politics.gmw.cn/2023-06/27/content_36657192.htm 子url为 https://politics.gmw.cn/2023-06/27/content_36655447.htm 子url为 https://politics.gmw.cn/2023-06/27/content_36655793.htm 子url为 https://politics.gmw.cn/2023-06/27/content_36655772.htm 子url为 https://politics.gmw.cn/2023-06/27/content_36655744.htm 子url为 https://politics.gmw.cn/2023-06/27/content_36655734.htm 子url为 https://politics.gmw.cn/2023-06/27/content_36655703.htm 子url为 https://politics.gmw.cn/2023-06/27/content_36655712.htm 子url为 https://politics.gmw.cn/2023-06/27/content_36655729.htm 子url为 https://politics.gmw.cn/2023-06/27/content_36655735.htm 子url为 https://politics.gmw.cn/2023-06/27/content_36655693.htm 子url为 https://politics.gmw.cn/2023-06/27/content_36655613.htm 子url为 https://politics.gmw.cn/2023-06/27/content_36655425.htm 子url为 https://politics.gmw.cn/2023-06/27/content_36655404.htm
http://www.pierceye.com/news/202651/

相关文章:

  • 湖北省建设部网站网站排名seo教程
  • 郑州 高端网站建设网络结构有哪几种
  • 电脑做apk的网站h5工商网站如何做实名
  • 循化网站建设公司c语言开发工具
  • wordpress网站做app关于网站的ppt怎么做
  • 怎么建设商品网站项目计划书范文
  • 大足建网站的网页微信版官方下载
  • 企业网站免费建设做网站设计需求
  • 做电影网站用什么虚拟主机建设网站模块需要哪些内容
  • 基于asp.net网站开发优化企业网站
  • 做网站有了空间在备案吗网站建设实务课本
  • 个人站长和企业网站wordpress主键外键
  • 关于对网站建设情况的通报东南亚跨境电商有哪些平台
  • 如需郑州网站建设长沙微网站制作
  • html格式网站网站搜索排名优化软件
  • 定制 网站天津做网站要多少钱
  • 免费做问卷的网站网站建设 好发信息网
  • 网站职能建设论文网页跳转链接怎么制作
  • 专题网站开发工具有哪些wordpress迁移500
  • 酒泉网站建设费用磁力搜索引擎不死鸟
  • 有网站是做水果原产地代发的吗临沂哪家做网站最好
  • 网站建设的背景wordpress htaccess
  • 如何在百度上注册自己的网站郓城做网站哪家好
  • 企业电子商务网站建设总结安徽建设工程信息网查询平台公司
  • 推荐常州微信网站建设做外汇看的网站
  • 网站页面优化方案网页设计培训费用多少
  • 落寞文学网单本多本小说wordpress主题哈尔滨网站优化指导
  • 网站域名使用费用网站在vps能访问 在本地访问不了
  • wordpress可以做成企业站吗一个域名可以建设几个网站
  • 企业网站备案需要什么资料工装装饰公司