【发布时间】:2018-04-08 16:42:27
【问题描述】:
我已经阅读了 10 多篇关于 print href, text 的帖子,但我找不到一篇同时打印 text 和 href 的帖子。
网站是https://cyware.com/cyber-security-news-articles
我要抓取文章的文字和网址
这是我的代码:
from urllib.request import urlopen
from bs4 import BeautifulSoup
page = urlopen("https://cyware.com/cyber-security-news-articles")
soup = BeautifulSoup(page, 'html5lib')
questions = soup.find_all('h2',{"class":"post post-v2 format-image news-card get-id"})
for h2 in soup.find_all('h2'):
print(h2.text)
print(h2.href)
但是href的结果是none。
我想知道为什么print(h2.href) 不打印链接。
问题包含 href="~~"
<a rel="nofollow" target="_blank" class="action_url" href="https://in.reuters.com/article/us-iran-cyber-hackers/iran-hit-by-global-cyber-attack-that-left-u-s-flag-on-screens-idINKBN1HE0MH">Iran hit by global cyber attack that left U.S. flag on screens with a warning “Don’t mess with our elections”</a>
【问题讨论】:
标签: python parsing beautifulsoup