2016-09-09 2 views
-2

안녕하세요 파이썬 3.5.2에 문제가 있습니다 atribute의 값을 가져 오려면 모든 태그를 가져올 때 어디에 문제가 있는지 알 수 없습니다. (atribute + value)하지만 나는 제목의 가치를 원하니 ?? 이 내 코드태그에서 속성 값을 얻는 방법 python을 사용하는 html 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) 

내가이

[<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

그냥 "A"태그의 텍스트를 원하는 경우 모든 웹 링크가 tagss에 저장되기 때문에, 단지 반복하고 인쇄를 얻을 수 같은 다음과 같습니다 :

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

대단히 감사합니다 –

관련 문제