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

佛山市建设官方网站电商学校学费多少钱

佛山市建设官方网站,电商学校学费多少钱,网页设计免费素材,淘宝详情页psd模板免费咱们这个任务啊,就是要从一个指定的网站上,抓取新闻内容,然后把它们整整齐齐地保存到本地。具体来说,就是要去光明网的板块里,瞅瞅里面的新闻,把它们一条条地保存下来。 首先,咱得有个网址&…

咱们这个任务啊,就是要从一个指定的网站上,抓取新闻内容,然后把它们整整齐齐地保存到本地。具体来说,就是要去光明网的板块里,瞅瞅里面的新闻,把它们一条条地保存下来。

首先,咱得有个网址,这就是咱要去的地方。然后用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('//*[@id="article_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', encoding='utf-8', errors='ignore') 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.yayakq.cn/news/879973/

相关文章:

  • 做qq空间网站河南省建设科技会网站
  • 服装网站的建设背景做网站一般几个步骤
  • 公司做网站需要什么wordpress 音乐网
  • 有没有网站可以做地图长安网站建设价格
  • 做兼职在什么网站上找大连网络营销公司有哪些
  • 网站开发 实训 报告开发网站服务
  • 怎样给网站做新闻稿子wordpress付费服务器
  • php网站后台搭建网站建设规划怎么写
  • 0基础学习网站开发局域网怎么做网站
  • 成都网站设计公司价格中国核工业二三建设有限公司招聘信息
  • 房地产管理网站做网站域名的成本
  • 做汽车团购的网站用什么软件做动漫视频网站
  • 1核1g可以做几个网站免费推广平台微信号
  • 网站域名列表做瞹瞹瞹视频免费网站
  • 网站开通银行支付接口广州商城网站建设地址
  • 介绍移动互联网的网站有哪些重庆金融公司网站建设
  • 中国建设银行官方网站企业网络推广的基本渠道
  • 明年做哪个网站能致富怎么找的做网站的人
  • 蛋糕店的网站建设咋写做网站都去哪申请网址
  • 建设网站怎么收费网站建设的公司怎么收费
  • 优酷wordpress建站教程做网站必备的注意事项
  • 站长要维护网站东莞做网站公司哪家好
  • 温州百度搜索优化win优化大师官网
  • c站网站页面优化内容包括哪些
  • php网站在线打包源码素材下载网站开发文档
  • 做导购网站多少钱盘县网站开发
  • 网站搭建平台价格杭州建设工程网
  • 嘉兴网站建设托管辽宁海星建设集团有限公司网站
  • 企业网站商城wordpress主题茶叶
  • 做网站设计提成赚钱吗小公司根本办不了icp许可证