Skip to Content
Python文本分析
book

Python文本分析

by Jens Albrecht, Sidharth Ramachandran, Christian Winkler
August 2022
Intermediate to advanced
441 pages
11h 26m
Chinese
China Electric Power Press Ltd.
Content preview from Python文本分析
84
3
要,因为我们可能被
Web
服务器封禁,或者遇到网络连接断开的情况。下面,我们
将上一个案例中获得的
URL
列表保存到一个文件中,作为下载页面的列表:
with open("urls.txt", "w+b") as f:
f.write("\n".join(urls).encode('utf-8'))
下面打开命令行(或
Jupyter
的终端页签),然后调用
wget
wget -nc -i urls.txt
选项
-i
告诉
wget
需要下载的
URL
列表。你可以观察一下,
wget
会跳过已有的文件(因
为我们指定了选项
-nc
),而且整个下载速度非常快。
如果指定选项
-r
wget
还可以递归下载页面。
小心被封杀!
请注意,长时间运行进程,有可能导致你被某个网站封杀。一般情况下,
在实验递归下载(
-r
项)时,最好指定参数
-l
(即指定递归层数)。
下载的数据有很多种。对于数量比较适中(几百至一千个)的网页来说,标准的做
法是直接通过
Python
程序下载。我们推荐
requests
库,因为该库非常易于使用。
如果需要下载的页面超过几千个,通常分阶段下载比较合适,首先生成
URL
列表,
然后通过专门的外部程序(比如
wget
)下载。
3.11
提取半结构化数据
在下一节中,我们将探讨从路透社的文章中提取数据的各种方法。首先,我们将使
用正则表达式,然后再介绍一款功能强大的
HTML
解析器。
一般,我们感兴趣的数据不仅来自于一篇文章,但是作为第一步,我们只介绍一篇
文章。我们将以“
Banned in Boston: Without ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

精益AI

精益AI

Lomit Patel
构建知识图谱

构建知识图谱

Jesus Barrasa, Jim Webber
写给系统管理员的Python脚本编程指南

写给系统管理员的Python脚本编程指南

Posts & Telecom Press, Ganesh Sanjiv Naik

Publisher Resources

ISBN: 9787519864446