程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了BS4没有找到li物品?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决BS4没有找到li物品??

开发过程中遇到BS4没有找到li物品?的问题如何解决?下面主要结合日常开发的经验,给出你关于BS4没有找到li物品?的解决方法建议,希望对你解决BS4没有找到li物品?有所启发或帮助;

我正在尝试使用请求和 bs4 从交易经济学中抓取文章数据。下面是我写的脚本:

import requests
import pandas as pd
from bs4 import BeautifulSoup

url = '@R_616_10107@s://Tradingeconomics.com/stream?c=country+List'
r = requests.get(url).text
parser = BeautifulSoup(r)
li = parser.find_all('li',class_='List-group-item')
print(li)

查看网页可以看到,每篇文章都在一个li item下,并且class='List-group-item'。奇怪的是,bs4 找不到任何 li 项,返回 None。可能是什么情况?

解决方法

您在页面上看到的数据是通过 JavaScript 动态加载的,因此 beautifulsoup 看不到它。您可以使用此示例模拟 javascript 请求:

import json
import requests

url = "@R_616_10107@s://tradingeconomics.com/ws/stream.ashx"
params = {"start": 0,"size": 20,"c": "country list"}
data = requests.get(url,params=params).json()

# uncomment this to print all data:
# print(json.dumps(data,indent=4))

for item in data:
    print(item["title"])
    print(item["description"])
    print(item["url"])
    print("-" * 80)

打印:

Week Ahead
Central banks in the US,Brazil,Japan,the UK and Turkey will be deciding on monetary policy in the coming week,while key data to watch for include US and China induStrial output and retail SALEs; Japan and Canada inflation data; UK consumer morale; Australia employment figures and retail trade; New Zealand Q4 GDP; and India wholeSALE prices.
/calendar?article=29075&g=top&importance=2&startdate=2021-03-12 
--------------------------------------------------------------------------------
Week Ahead
The US Senate will be debaTing and voTing on the President Biden's $1.9 trillion coronavirus aid bill during the weekend,and send it BACk to the House to be signed into law as soon as next week. Elsewhere,monetary policy meeTings in the Eurozone and Canada will be keenly watched,as well as updated GDP figures for Japan,the Eurozone,the UK and South Africa. Other important releases include US,China and India inflation data; US and Australia consumer sentiment; UK and China foreign trade; Eurozone and India induStrial output; and Japan current account.
/calendar?article=29072&g=top&importance=2&startdate=2021-03-05 
--------------------------------------------------------------------------------
Week Ahead
After President Biden's $1.9 trillion pandemic aid bill narrowly passed the House in the early hours of Saturday,all attention would move now to vote in Senate. Meanwhile,investors will also turn the eyes to US jobs report due Friday; GDP data for Australia,Canada and Turkey as well as worldwide manufacturing and services PMI surveys and monetary policy action by the Reserve Bank of Australia. Other releases include trade figures for the US and Canada,factory orders for the US and GeRMANy,unemployment rate for the Euro Area and Japan,and induStrial output and retail SALEs for South Korea.
/calendar?article=29071&g=top&importance=2&startdate=2021-02-27 
--------------------------------------------------------------------------------

...and so on.

大佬总结

以上是大佬教程为你收集整理的BS4没有找到li物品?全部内容,希望文章能够帮你解决BS4没有找到li物品?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: