我正在尝试抓取https://www.forbes.com/网站首页文章的链接和标题。
我并不精通html,但我一直在关注一些关于美人汤的教程,并且一直在用我一路学到的知识过活。
这是我到目前为止所知道的:
source = urllib.request.urlopen('https://www.forbes.com').read()
soup = bs.BeautifulSoup(source,'lxml') # Tried 'html.parser' as well
##print(soup.findAll('div',{'class':"c-entry-box--compact c-entry-box--compact--article"}))
for url in soup.findAll('a',{'class':"exit_trigger_set"}):
print (url.get('href'))检查网站的html,我似乎有类和'a‘(不确定你在这种情况下叫'a’)是正确的。
然而,我只得到了一个,而不是获得首页上所有文章的链接。
https://www.amazon.com/Intelligent-REIT-Investor-Wealth-Investment/dp/1119252717
不知道我做错了什么。
谢谢。
编辑:
这似乎找到了一些头条新闻,但我不知道如何只拉出链接
for i in soup.findAll('h4', {'class': "editable editable-hed"}):
print (i)发布于 2017-01-31 16:41:18
下面是我会怎么做:
import urllib2
from bs4 import BeautifulSoup
import pandas as pd
source = urllib2.urlopen('https://www.forbes.com')
soup = BeautifulSoup(source,'lxml')
lst = []
for i in soup.findAll('h4', {'class': "editable editable-hed"}):
title = i.text
link = i.find('a')['href'][2:]
title = title.replace('\t','')
title = title.replace('\n','')
title = title.strip()
lst.append({'title':title, 'link':link})
df = pd.DataFrame.from_dict(lst)你会得到15篇文章和它们的链接。
https://stackoverflow.com/questions/41876635
复制相似问题