【问题标题】:BeautifulSoup not picking up text from span class or section class tagsBeautifulSoup 没有从跨度类或部分类标签中提取文本
【发布时间】:2019-11-07 12:04:40
【问题描述】:

我无法从该页面打印出文本,因为 BeautifulSoup 没有选择跨度类或部分类标签。我想从 Motley Fool 中提取文本,然后逐句解析。

https://www.fool.com/earnings/call-transcripts/2019/04/26/exxon-mobil-corp-xom-q1-2019-earnings-conference-c.aspx

到目前为止,当它偶尔拉入文本时,句子解析工作,然而,美丽的汤只是偶尔拉入文本。

from textblob import TextBlob
from html.parser import HTMLParser
import re
def news(): 
    # the target we want to open     
    url = dataframe_url

    #open with GET method 
    resp=requests.get(url) 

    #http_respone 200 means OK status 
    if resp.status_code==200: 

        soup = BeautifulSoup(resp.text,"html.parser")

        #l = soup.find("span",attrs={'class':"article-content"})
        l = soup.find("section",attrs={'class':"usmf-new article-body"})

        #print ('\n-----\n'.join(tokenizer.tokenize(l.text)))
        textlist.extend(tokenizer.tokenize(l.text))

    else: 
        print("Error")

【问题讨论】:

  • 你能举几个没有被拉出的句子的例子吗?另外,您是否尝试过soup.find_all() 而不是soup.find()
  • 我正在尝试从 url 中提取整个脚本文本,这是 html 中的一个例句:

    大家好。欢迎参加埃克森美孚公司 2019 年第一季度财报电话会议。正在录制今天的通话。

标签: python beautifulsoup tags html-parsing textblob


【解决方案1】:

为了捕获成绩单,您可以尝试这样的方法 - 并根据您的需要进行修改:

import requests
from bs4 import BeautifulSoup as bs

with requests.Session() as s:
    response = s.get('https://www.fool.com/earnings/call-transcripts/2019/04/26/exxon-mobil-corp-xom-q1-2019-earnings-conference-c.aspx')

soup = bs(response.content, 'lxml')

heads = soup.find_all('h2')
selections = ['Prepared Remarks:','Questions and Answers:']

for selection in selections:
    for head in heads:
        if head.text == selection:
            for elem in head.findAllNext():
                if elem.name != 'script':                    
                    print(elem.text)
                if 'Duration' in elem.text:
                    break

如果距离够近,请告诉我。

【讨论】:

  • 感谢您的帮助,因为我发现 Motley Fool 将我的 IP 列入了黑名单,因为我收到了 Max Retries SSL 错误。我猜他们真的不喜欢网络抓取,因为我过去也遇到过类似的错误!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多