2011-06-29 5 views
0

私はドキュメントを見てきましたが、この問題は扱いません。私はすべてのテキストとすべてのリンクを抽出しようとしていますが、別々にはできません。私はそれらをインタリーブして文脈を維持してほしい。私は、テキストとリンクのインターリーブされたリストで終わりたいです。これはBeautifulSoupでも可能ですか?美しいスープを使って複数のアイテムを抽出することは可能ですか?

答えて

0

はい、間違いありません。

ダウンこのコードスニペットを破る
import urllib2 
import BeautifulSoup 

request = urllib2.Request("http://www.example.com") 
response = urllib2.urlopen(request) 
soup = BeautifulSoup.BeautifulSoup(response) 
for a in soup.findAll('a'): 
    print a 

、あなたは(この場合はGoogle.comで)ウェブサイトのためrequestを作り、BeautifulSoupでresponseバックを解析しています。あなたの要件は、すべてのリンクとテキストを見つけて、文脈を守ることでした。上記のコードの出力は次のようになります:

<a href="/"><img src="/_img/iana-logo-pageheader.png" alt="Homepage" /></a> 
<a href="/domains/">Domains</a> 
<a href="/numbers/">Numbers</a> 
<a href="/protocols/">Protocols</a> 
<a href="/about/">About IANA</a> 
<a href="/go/rfc2606">RFC 2606</a> 
<a href="/about/">About</a> 
<a href="/about/presentations/">Presentations</a> 
<a href="/about/performance/">Performance</a> 
<a href="/reports/">Reports</a> 
<a href="/domains/">Domains</a> 
<a href="/domains/root/">Root Zone</a> 
<a href="/domains/int/">.INT</a> 
<a href="/domains/arpa/">.ARPA</a> 
<a href="/domains/idn-tables/">IDN Repository</a> 
<a href="/protocols/">Protocols</a> 
<a href="/numbers/">Number Resources</a> 
<a href="/abuse/">Abuse Information</a> 
<a href="http://www.icann.org/">Internet Corporation for Assigned Names and Numbers</a> 
<a href="mailto:[email protected]?subject=General%20website%20feedback">[email protected]</a> 
関連する問題