【问题标题】:BeautifulSoup find_all(text=True) not printing anything?BeautifulSoup find_all(text=True) 没有打印任何东西?
【发布时间】:2021-02-17 15:59:21
【问题描述】:

我要做的就是从所有

中获取文本,但它不返回任何内容。
from bs4 import BeautifulSoup
import requests

for page_no in range(1, 3):
    data = {
        'filterToken': "",
        'instrumentType': "EQUITY",
        'maxResultsPerPage': 10,
        'pageNumber': page_no
    }
    page = requests.post('https://www.nyse.com/api/quotes/filter', data=data)
    soup = BeautifulSoup(page.text, 'html.parser')

    print('PAGE', page_no)
    

    for name in soup.findAll('td'):
        print(''.join(name.findAll(text=True)))

我没有收到任何错误,它只打印页码。我知道已经有几个 find_all 问题,但似乎都没有改变结果,也许我的“请求”是错误的?非常感谢任何帮助。

谢谢

【问题讨论】:

  • 您是否尝试过打印页面以查看它的外观?或者通过'<td' in page 确保至少有一个“td”?
  • 检查你的汤 -> HTTP Status 415 – Unsupported Media Type -- The origin server is refusing to service the request because the payload is in a format not supported by this method on the target resource 并修复你的有效载荷问题。
  • @HedgeHog 好的,我正在调查它,会回复解决方案。谢谢

标签: python beautifulsoup findall


【解决方案1】:

好吧,既然这是一个有效负载问题,我决定继续使用邮递员让它在那里工作并从代码生成器中获取 sn-p。

这是我从邮递员那里得到的代码(除了我添加的用于更改页面的 for 循环)

我把汤保持原样。这绝对是有效载荷格式的问题。

from bs4 import BeautifulSoup
import requests


url = "https://www.nyse.com/api/quotes/filter"

for page_no in range(1,3):    
    payload="{\r\n\"filterToken\": \"\",\r\n\"instrumentType\": \"EQUITY\",\r\n\"maxResultsPerPage\": 10,\r\n\"pageNumber\": 2,\r\n\"sortColumn\": \"NORMALIZED_TICKER\",\r\n\"sortOrder\": \"ASC\"\r\n}"
    headers = {
      'Content-Type': 'application/json',
      'Cookie': '__cfduid=d1d98afcb22d3de94fabd5aa239a7f9dd1613587281; ICE=!JZg6hDNDz8MeToWQmW/NR4Un8KL87Pjjm4sZeq90MTiHJNFVEjMb6jE17JMo4Kqh30d5/r5S1JWaRw==; TS01ebd031=0100e6d4952c533b3a410d9219c173dd7215532f3c5eb2aa18ab3c24ed0e7523be4ae96305fe495c8f3ced04071d353afb732ef83a06090cec6f6dc7d40a1c2c7dcb86a73e'
    }   

    response = requests.request("POST", url, headers=headers, data=payload)

    print(response.text)
    print('PAGE:', page_no)
    soup = BeautifulSoup(url, 'html.parser')

    for symbol in soup.findAll('td'):
        print(''.join(symbol.findAll(text=True)))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多