惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
Recorded Future
Recorded Future
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google DeepMind News
Google DeepMind News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Microsoft Azure Blog
Microsoft Azure Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
M
MIT News - Artificial intelligence
云风的 BLOG
云风的 BLOG
Y
Y Combinator Blog
N
News | PayPal Newsroom
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Help Net Security
Help Net Security
博客园 - Franky
SecWiki News
SecWiki News
Recent Announcements
Recent Announcements
T
Troy Hunt's Blog
The Register - Security
The Register - Security
The Last Watchdog
The Last Watchdog
Webroot Blog
Webroot Blog
S
Security Affairs
博客园 - 司徒正美
S
Schneier on Security
I
InfoQ
博客园_首页
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Threat Research - Cisco Blogs
Forbes - Security
Forbes - Security
腾讯CDC
N
Netflix TechBlog - Medium
N
News and Events Feed by Topic
Cloudbric
Cloudbric
T
The Exploit Database - CXSecurity.com
P
Proofpoint News Feed
A
About on SuperTechFans
Engineering at Meta
Engineering at Meta
Recent Commits to openclaw:main
Recent Commits to openclaw:main
B
Blog
V
Vulnerabilities – Threatpost
C
Check Point Blog
Google DeepMind News
Google DeepMind News
Google Online Security Blog
Google Online Security Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
Cisco Blogs
Schneier on Security
Schneier on Security
O
OpenAI News
K
Kaspersky official blog

博客园 - pmh905001

爬虫-今日头条我的收藏-反爬虫分析(六) pycharm的没落,vs code的兴起 爬虫-今日头条我的收藏-增量式导入到Elastic Search(四) 今日头条源代块一行代码很长情况下的拖动问题 爬虫-今日头条我的收藏-增量式导入到mongodb(三) 爬虫-今日头条我的收藏-增量式(二) openpyxl一个bug 爬虫-今日头条我的收藏(一) pystray被隐藏菜单项显示出来的问题 pyinstaller生成的exe程序使用使用默认程序打开execel文件 pyinstaller生成的exe文件的所在的工作目录问题 windows下python的keyboard库在锁屏之后再次登陆快捷键(热键)失效问题 pyinstaller 报错ImportError: No module named _bootlocale windows下gitbash 鼠标左键选中文字自动自行终止命令 ctrl+c ^C spring-security 如何使用用户名或邮箱登录 tomcat jndi context.xml的特殊字符转义问题 struts2的优缺点以及如何改造 jetty-maven-plugin 版本导致jetty启动失败问题 Eclipse下pom.xml的提示 Cannot access defaults field of Properties
爬虫-今日头条我的收藏-增量式下载网页内容(五)
pmh905001 · 2024-03-29 · via 博客园 - pmh905001

背景:

原先我们下载的文件包含了收藏的元信息,包含标题,链接,文章摘要信息。这些基本的信息就足够支撑查询功能了。但还是存在如下问题:

  • 从业务上来说,文章的原作者可能会删除文章,那么收藏的文章将再也找到相关信息了。所以我们需要把这些信息也要保存。
  • 如果用户标题以及摘要信息不全面,重要信息在文章里面。那么关键字同样查询不到需要的信息。

所以,我们还是需要把网页的内容保存下来。

方案:

  • 需要根据元信息里面的链接去把html内容保存下来。
  • 元信息之前是存储在文件里面,html内容暂定还是保存为文件。每行的是一个json数据,key是id信息,value是html内容。 也考虑用id: html内容保存的形式,但是发现下载下来的html内容有多行的。会给解析造成一些麻烦。
  • 下载html内容可以使用requests,selenium,aiohttp异步方式下载。
    1. aiohttp异步方式虽然很快,但是会出现发送请求太频繁,服务端断连接的情况。有点头条的监控系统导致账号被锁。
    2. selenium的问题是它需要打开一个chrome浏览器,需要安装对应的chromedriver.exe,需要考虑等待时间让内容渲染,这对于抓取网页内容不准确。
    3. requests需要一个一个下载,对于这种io频繁无法复用协程.
  • 最终还是选择使用requests,原因在于这样比较安全,不会导致账号被锁,我也确实不知道头条会不会这么做?
  • aiohttps其实有代码实现,等把这么多的html内容下载下来再去验证至少稳妥一些。
  • 也需要支持断点功能下载功能,这么做的目的:文件比较大情况下,如果下载到一半,由重新下载,无疑会消耗很多时间,因此需要找到上次下载最后一条记录的id,反向查找元信息文件,从这个位置继续下载html内容。

遇到的问题:

  • 元信息里面的链接是需要301跳转,大约跳转三次才能得到真正的地址。request本身allow_redirects=True的情况无法通过得到真正的html内容。估计的是头条的保护措施。以后有时间再谢谢它redirect的过程
  • requests发送请求的时候,会添加一个header信息,实际上只有cookie信息是有用的,其他信息无用。
  • 收藏文章的url信息是分散在不同的字段里面,需要有判断逻辑。
  • 找断点的这个逻辑写起来要麻烦一点,代码需要重构一下。
  • cookie.txt文件涉及用户隐私,需要单独配置一个文件。

代码:

https://gitee.com/pmh905001/myfavorite/blob/master/toutiao/html_downloader_requests.py