如何使用beautifulsoup获取重定向html? [英] How to use beautifulsoup to get redirect html?

查看:187
本文介绍了如何使用beautifulsoup获取重定向html?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看带有以下标头的网络文件.如何使用bs4获取google.com页面的内容?

I'm looking at a web file with the following header. How could I get the content of google.com page with bs4?

<head>
<meta http-equiv="refresh" content="5;url=http://google.com"/>  
</head>

谢谢!

推荐答案

使用标记名称为metafind,并且具有已知固定属性的attrs,即http-equiv的值必须为refresh .从结果集中获取第一个此类元素,并获取其'content'属性的值,然后将其解析为url.

Use find with tag name meta, and attrs having the known fixed attribute, namely http-equiv needs to have value of refresh. Get the first such element from the result set, and take the value of its 'content' attribute, then parse it for url.

因此,您得到:

>>> fragment = """<head><meta http-equiv="refresh" content="5;url=http://google.com"/></head>"""
>>> soup = BeautifulSoup(fragment)
>>> element = soup.find('meta', attrs={'http-equiv': 'refresh'})
>>> element
<meta content="5;url=http://google.com" http-equiv="refresh"/>

>>> refresh_content = element['content']
>>> refresh_content
u'5;url=http://google.com'

>>> url = refresh_content.partition('=')[2]
>>> url
u'http://google.com'

这篇关于如何使用beautifulsoup获取重定向html?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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