pizzal0ver 2020-11-25 13:19 采纳率: 0%
浏览 82

Python IDLE 如何爬取新浪新闻网站上的所有链接

用IDLE,我写的代码可以爬,但是只能爬到https://news.sina.com.cn/为前缀的文字内容,包括video,slids啥的都爬不到,这个咋改一下可以爬取全部链接啊。

以下是代码段

 

 

import re
import os
import requests

res=requests.get("http://roll.news.sina.com.cn/news/gnxw/gdxw1/index.shtml")
res=requests.get("https://news.sina.com.cn/china/")
#news.sina.com.cn/c/2020-11-21/doc-iiznezxs2947425.shtml
res.encoding = 'utf-8'
print(res.text)

urllist=re.findall('<a href="https://news.sina.com.cn/[a-z]{1}/[a-zA-Z0-9/-]+.shtml"', res.text)

print(len(urllist))

#<a href="http://news.sina.com.cn/c/nd/2018-09-12/doc-ihiycyfx5412189.shtml"
for u in urllist[0:10]:
   url=u[9:len(u)-1]
   print(url)
   res=requests.get(url)
   res.encoding='utf-8'
   #http://news.sina.com.cn/o/2018-10-19/doc-ifxeuwws5952620.shtml
   #得到文件名doc-ifxeuwws5952620.shtml
   filename=re.findall("/[a-zA-Z0-9-.]+$", url)
   f=open("sinanews\\"+filename[0][1:],"wb")
   f.write(res.text.encode("utf-8"))   #html原始文档内容
   f.close()

print(len(urllist))
 

  • 写回答

1条回答 默认 最新

  • 清风未明月 2020-11-26 11:22
    关注

    你这个匹配的就匹配到https://news.sina.com.cn/为前缀的文字内容啊,要想爬其他的还得设置其他的匹配代码

    评论

报告相同问题?

悬赏问题

  • ¥15 vue2(标签-chrome|关键词-浏览器兼容)
  • ¥15 python网络流自动生成系统 医学领域
  • ¥15 sql查询仓库里都有什么
  • ¥15 代码的修改,添加和运行完善
  • ¥15 krpano-场景分组和自定义地图分组
  • ¥15 lammps Gpu加速出错
  • ¥15 关于PLUS模型中kapaa值的问题
  • ¥15 关于博途V17进行仿真时无法建立连接问题
  • ¥15 机器学习教材中的例题询问
  • ¥15 求.net core 几款免费的pdf编辑器