我有一个简单的任务,可以从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] 删除。
我来说两句