使用python上的bs4在WSJ上进行Web抓取

时间:2020-04-23 21:03:25

标签: python beautifulsoup

我正在尝试使用bs4和请求模块从WSJ的首页上抓取基本信息。

主要是第一篇文章的标题和SP500索引号。

但是,对于我的代码,它没有显示错误消息,但仍然没有输出。如何格式化我的代码以显示标题和索引号?我的代码如下:

import bs4
import requests

def WSJ():
    res = requests.get('https://www.wsj.com/')
    soup = bs4.BeautifulSoup(res.text, 'html.parser')
    elems = soup.select('document.querySelector("#top-news > div > div.style--grid--SxS2So51.style-- 
    full-width--105xgnPD.style--padding-bottom--2Q75WlSu.styles--padding-bottom--DQ6elYpe.style-- 
    margin-bottom--3uv3Nt9X.style--border-bottom--TyDVcRcy.styles--border-bottom--2gLRRJBY > div:nth- 
    child(1) > article:nth-child(1) > div > h3")')

    print('The top headline today on WSJ is:')
    print(elems)

    elems1 = soup.select('document.querySelector("#market-data > div.WSJTheme--marketsData- 
    instruments--3rwut0On > div:nth-child(2) > div.WSJTheme--marketsData-quote--FsY5B3tc.WSJTheme-- 
    marketsData-quote-tick--3d3FL88h")')
    print('The S&P 500 index today is at:')
    sp = int((elems1[0].text.strip()))
    print(sp)

WSJ()

0 个答案:

没有答案