简体   繁体   English

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

[英]Extracting links with href attribute in Python BeautifulSoup

I have a simple task to extract links from html (url). 我有一个简单的任务,可以从html(url)中提取链接。 I do this: 我这样做:

> #!/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!'

and this is the html: 这是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>

Any idea why everything my script returns is: "Hey there!"? 为什么我的脚本返回的所有内容都是:“嘿!”? If I modify my code to: 如果我将代码修改为:

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

All I get is: 我得到的是:

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

Why, where is "href" attribute? 为什么“ href”属性在哪里?

I tested you HTML code using BeautifulSoup 4: 我使用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

You have a spelling mistake: 您有一个拼写错误:

for item in soup.find_all(herf=True):

It should be href: 应该是href:

for item in soup.find_all(href=True):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM