我有一个简单的任务从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”属性在哪里?
答案 0 :(得分:1)
我使用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
答案 1 :(得分:0)
你有拼写错误:
for item in soup.find_all(herf=True):
应该是href:
for item in soup.find_all(href=True):