2016-09-09 30 views
-2

こんにちは、私はpython 3.5.2に問題があります 私はatributeの値を取得するときに問題がどこにあるのかわかりませんすべてのタグを取得する(atribute + value)しかし、私はタイトルの価値だけを必要としますか? これは私のコードタグから属性値を取得する方法html python 3.5.2

from bs4 import BeautifulSoup as bs 
import requests 

url = "http://bestofgeeks.com/en/" 
html = requests.get(url).text 
soup = bs(html,'html.parser') 

tagss = soup.findAll('a',{'class':'titre_post'}) 
print(tagss) 

で、あなただけの「」タグ、すべてのウェブリンクがtagssに格納されているので、ちょうど反復処理し、印刷からテキストをしたい場合、私はこの

[<a charset="UTF-8" class="titre_post" href="article_to_read.php?category=Last-Technology&amp;name=854&amp;title=Apple-Watch-Series-2-Waterproof-50-meters-with-Pokemon-Go" hreflang="en" rel="tag" titre="Apple Watch Series 2 Waterproof 50 meters with Pokemon Go"> 
Apple Watch Series 2 Waterproof 50 meters with Pokemon Go  </a>, <a charset="UTF-8" class="titre_post" href="article_to_read.php?category=Security&amp;name=853&amp;title=Warning-This-Cross-Platform-Malware-Can-Hack-Windows-Linux-and-OS-X-Computers" hreflang="en" rel="tag" titre="Warning This Cross Platform Malware Can Hack Windows Linux and OS X Computers"> 
Warning This Cross Platform Malware Can Hack Windows Linux and OS X Computers  </a>, <a charset="UTF-8" class="titre_post" href="article_to_read.php?category=Games&amp;name=852&amp;title=PS4-Slim-Announced,-Launching-This-Month-coming-september-15-for-299$-" hreflang="en" rel="tag" titre="PS4 Slim Announced, Launching This Month coming september 15 for 299$ "> 
PS4 Slim Announced, Launching This Month coming september 15 for 299$  </a>, <a charset="UTF-8" class="titre_post" href="article_to_read.php?category=Last-Technology&amp;name=851&amp;title=Sony-New-IFA-products" hreflang="en" rel="tag" titre="Sony New IFA products"> 
Sony New IFA products  </a>, <a charset="UTF-8" class="titre_post" href="article_to_read.php?category=Phone&amp;name=850&amp;title=This-is-the-iPhone-7-waterproofing,-stereo-speakers,-and-dual-cameras" hreflang="en" rel="tag" titre="This is the iPhone 7 waterproofing, stereo speakers, and dual cameras"> 
This is the iPhone 7 waterproofing, stereo speakers, and dual cameras  </a>, <a charset="UTF-8" class="titre_post" href="article_to_read.php?category=Security&amp;name=849&amp;title=Russia-is-Largest-Portal-HACKED;-Nearly-100-Million-Plaintext-Passwords-Leaked" hreflang="en" rel="tag" titre="Russia is Largest Portal HACKED; Nearly 100 Million Plaintext Passwords Leaked"> 
Russia is Largest Portal HACKED; Nearly 100 Million Plaintext Passwords Leaked  </a>] 
+0

このコードは期待どおり動作します。あなたの希望する出力は何ですか? – DeepSpace

答えて

0

を取得

for t in tagss: 
    print t.text.strip() 
+0

大変ありがとうございます –

0

あなたはtitre属性の内容を望む意味場合:

、以下に示すような
tagss = [tag.get('titre') for tag in soup.findAll('a',{'class':'titre_post'})] 
+0

ありがとう –

関連する問題