在Python中检索一个网页的标题 [英] retrieving just the title of a webpage in python

查看:269
本文介绍了在Python中检索一个网页的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有超过5000个网页,我想要所有这些标题。在我的项目中,我使用了BeautifulSoup html解析器。

I have more than 5000 webpages i want the titles of all of them. In my project i am using the BeautifulSoup html parser like this.

soup = BeautifulSoup(open(url).read())
soup('title')[0].string

但它耗费大量时间。只是为了网页的标题,我正在阅读整个文件并构建解析树(我认为这是延迟的原因,如果我错了,请纠正我)。

But its taking lots of time. Just for the title of a webpage i am reading the entire file and building the parse tree(I thought this is the reason for delay, correct me if i am wrong).

是否有任何其他简单的方法来做到这一点在Python中。

Is there in any other simple way to do this in python.

推荐答案

它肯定会更快,如果你只是使用一个简单的正则表达式, BeautifulSoup 非常慢。你可以这样做:

It would certainly be faster if you just used a simple regular expression, BeautifulSoup is pretty slow. You could do something like:

import re
regex = re.compile('<title>(.*?)</title>', re.IGNORECASE|re.DOTALL)
regex.search(string_to_search).group(1)

这篇关于在Python中检索一个网页的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆