如何更改代码以从HTML代码获取URL链接?

时间:2019-04-07 02:02:37

标签: python-3.x web-scraping beautifulsoup web-crawler

我尝试使用beautifulsoup4在python中抓取HTML代码的URL,但是出现了这样的错误:AttributeError:'NoneType'对象没有属性'get'

HTML代码:

<a class="top NQHJEb dfhHve" href="https://globalnews.ca/news/5137005/donald-trump-robert-mueller-report/" ping="/url?sa=t&amp;source=web&amp;rct=j&amp;url=https://globalnews.ca/news/5137005/donald-trump-robert-mueller-report/&amp;ved=0ahUKEwiS9pn-4rzhAhWOyIMKHSOPD6QQvIgBCDcwAg"><img class="th BbeB2d" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ_Nf-kVlqsQz8NeNgQ9a9YRiA7Fl4DJ6Jod0sxNXapOK_iJebx20dgROk5YBl8IqFQX6S-eeY2" alt="Story image for trump from Globalnews.ca" onload="typeof google==='object'&amp;&amp;google.aft&amp;&amp;google.aft(this)" data-iml="1554598687532" data-atf="3"></a>

我的python代码:

URL_results = soup.find_all('a', class_= 'top NQHJEb dfhHve').get('href')

1 个答案:

答案 0 :(得分:0)

您正在将该方法应用于列表。相反,您想应用到每个元素

URL_results = [a.attrs.get('href') for a in soup.find_all('a', class_= 'top NQHJEb dfhHve')]

我喜欢

URL_results = [item['href'] for item in soup.select('a.top.NQHJEb.dfhHve')]

您也许可以从当前的复合类选择器中删除某些类,例如

URL_results = [item['href'] for item in soup.select('a.dfhHve')]

您需要四处逛逛,看看。