在Python BeautifulSoup中提取具有href属性的链接

帕维尔·休斯卡(Pawel Huszcza)

我有一个简单的任务,可以从html(url)中提取链接。我这样做:

> #!/usr/bin/python
> 
> import urllib import webbrowser from bs4 import BeautifulSoup
> 
> URL = "http://54.75.225.110/quiz" URL_end = "/question"
> 
> LINK = URL + URL_end file =
> urllib.urlopen("http://54.75.225.110/quiz/question") soup =
> BeautifulSoup(file)
> 
> for item in soup.find_all(href=True):
>     print item
> 
> 
> print 'Hey there!'

这是HTML:

> <html><head><meta http-equiv="Content-Type" content="text/html;
> charset=ISO-8859-1"> <script
> src="./question_files/jquery.min.js"></script> <script
> type="text/javascript">
>        function n(s) {
>               var m = 0;
>               if (s.length == 0) return m;
>               for (i = 0; i < s.length; ++i) {
>                         o = s.charCodeAt(i);          m = ((m<<5)-m)+o;           m = m & m;
>               }
>         return m;
>        };
>        $(document).ready(function() {
>                document.cookie = "client_time=" + (+new Date());
>                $(".x").attr("href", "./answer/"+n($("p[id|='magic_number']").text()));
>        }); </script> </head> <body> <p> <a class="x" style="pointer-events: none;cursor: default;"
> href="http://54.75.225.110/quiz/answer/56595">this page</a> (be
> quick). </p>

为什么我的脚本返回的所有内容都知道:“嘿!”?如果我将代码修改为:

for item in soup.find_all('a'): print item

我得到的是:

> <a class="x" style="pointer-events: none;cursor: default;">this
> page</a>

为什么“ href”属性在哪里?

意大利辣香肠比萨饼

我使用BeautifulSoup 4测试了您的HTML代码:

from bs4 import BeautifulSoup

soup = BeautifulSoup(html)

for a in soup.find_all('a'):
    if 'href' in a.attrs:
        print a['href']


http://54.75.225.110/quiz/answer/56595

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Python beautifulsoup中提取tr值的href链接

使用Python中的BeautifulSoup获取具有特定类属性的链接的href文本

使用 python 从具有下载选项的 <a> 中提取 href

如何从a标签中提取所有href,并测试它们是否以'/'开头或不使用beautifulsoup?-Python

Python如何在BeautifulSoup中提取具有相同类名的数据

如何使用Python在BeautifulSoup中的同一div中提取具有相同标签的元素?

在python中提取href

使用 Python 抓取网页时从链接中提取 href

从python3中具有多个属性的对象列表中提取多个属性列表

Python_BeautifulSoup:从 html 文件中提取属性数据

使用 BeautifulSoup 和 Python 从多个标签中提取文本,例如具有类的 h1 和 p 标签

Python 从 .txt 文件中提取所有的 href

python beautifulsoup解析'a'标签和href时没有链接

Python BeautifulSoup提取PHP链接

如何使用python中的BeautifulSoup包从网站中提取href内容

Python / BeautifulSoup:检索“ href”属性

从具有纯文本属性的'a'元素中提取href

我需要使用Python / BeautifulSoup从网页中提取嵌入式.xlsx链接的帮助

正则表达式使用Python从HTML的href属性中提取URL

Python BeautifulSoup从标题中提取数据

Python BeautifulSoup从结果中提取文本

Python - BeautifulSoup 从多个选项中提取值

Python - BeautifulSoup 从输入中提取值

从python beautifulsoup中的html中提取json

无法在python中提取URL链接

python从RSS提要中提取链接

使用python scrapy从网页中提取链接

Python:从tar.gz中提取具有模式的特定文件,而无需提取完整文件

使用Beam IO ReadFromPubSub模块时,可以在Python中提取具有属性的消息吗?尚不清楚它是否支持