Python 为什么我没有得到输出?

Python 为什么我没有得到输出?,python,beautifulsoup,python-requests,Python,Beautifulsoup,Python Requests,我有一个非常简单的问题-为什么我没有得到输出?这是网站:。我原以为问题是类名中的空格,但事实证明它是自然的,不应该引起任何问题。如果您有任何问题,请在评论中告诉我 import requests from bs4 import BeautifulSoup r = requests.get("https://riven.market/list/PC/Veiled") c = r.content soup = BeautifulSoup(c, "html.parser") all = soup

我有一个非常简单的问题-为什么我没有得到输出?这是网站:。我原以为问题是类名中的空格,但事实证明它是自然的,不应该引起任何问题。如果您有任何问题,请在评论中告诉我

import requests
from bs4 import BeautifulSoup

r = requests.get("https://riven.market/list/PC/Veiled")

c = r.content

soup = BeautifulSoup(c, "html.parser")
all = soup.find_all("div", {"class":"riven-list" })
for item in all:
    print("Name" + item.find("div", {"class": "attribute weapon"}).text.replace("\n", "").replace(" ", ""))

不确定您希望输出的内容,但您需要对请求url进行一些修改,并添加查询参数:

import requests
from bs4 import BeautifulSoup

url = 'https://riven.market/_modules/riven/showrivens.php'

payload = {
'baseurl': 'Lw==',
'platform': 'PC',
'limit': '25',
'recency': '-1',
'veiled': 'true',
'onlinefirst': 'true',
'polarity': 'all',
'rank': 'all',
'mastery': '16',
'weapon': 'Any',
'stats': 'Any',
'neg': 'all',
'price': '99999',
'rerolls': '-1',
'sort': 'time',
'direction': 'ASC',
'page': '1',
'time': '1565851478713'}

r = requests.get(url, params=payload)

c = r.content

soup = BeautifulSoup(c, "html.parser")
all_divs = soup.find_all("div", {"class":"riven " })
for item in all_divs:
    print("Name" + item.find("div", {"class": "attribute weapon"}).text.replace("\n", "").replace(" ", ""))
输出:

newtolRivenMod
>1daylRivenMod
>1daylRivenMod
newtolRivenMod
newtolRivenMod
>1daylRivenMod
>1daylRivenMod
>1daylRivenMod
>1dayRivenMod
>1dayunRivenMod
>1daylRivenMod
>1daylRivenMod
>1weekRivenMod
>1weekivenMod
>1weekivenMod
>1weeknRivenMod
>1weekRivenMod
>1weekRivenMod
>1weekRivenMod
>1weekRivenMod
>1weekivenMod
>1weekivenMod
>1weekivenMod
>1weeknRivenMod
>1weekivenMod

下面是我的解决方法,即查找查询将请求发送到的实际URL。您可以通过
右键单击
->
检查元素
->
网络
->
查找获取请求

输出


        Pistol Riven Mod
        new

        Pistol Riven Mod
        > 1 day

        Pistol Riven Mod
        > 1 day

        Pistol Riven Mod
        new

        Pistol Riven Mod
        new

        Pistol Riven Mod
        > 1 day

        Pistol Riven Mod
        > 1 day

        Pistol Riven Mod
        > 1 day

        Melee Riven Mod
        > 1 day

        Shotgun Riven Mod
        > 1 day

        Pistol Riven Mod
        > 1 day

        Pistol Riven Mod
        > 1 day

        Pistol Riven Mod
        > 1 week

        Melee Riven Mod
        > 1 week

        Rifle Riven Mod
        > 1 week

        Shotgun Riven Mod
        > 1 week

        Pistol Riven Mod
        > 1 week

        Pistol Riven Mod
        > 1 week

        Pistol Riven Mod
        > 1 week

        Pistol Riven Mod
        > 1 week

        Rifle Riven Mod
        > 1 week

        Rifle Riven Mod
        > 1 week

        Melee Riven Mod
        > 1 week

        Shotgun Riven Mod
        > 1 week

        Rifle Riven Mod
        > 1 week

没有带有class
riven list的
标记,该页面没有这样的div。不要使用inspector来删除项目,因为它会误导您。只使用“查看源代码”,但不知怎么的,它仍然不能work@Stasiek它不起作用,因为如果查看页面源代码,则没有带有
驱动列表的
标记。正如Selcuk所说,不要使用inspector对象来获取元素的ID,而是查看页面源代码来获取它。

        Pistol Riven Mod
        new

        Pistol Riven Mod
        > 1 day

        Pistol Riven Mod
        > 1 day

        Pistol Riven Mod
        new

        Pistol Riven Mod
        new

        Pistol Riven Mod
        > 1 day

        Pistol Riven Mod
        > 1 day

        Pistol Riven Mod
        > 1 day

        Melee Riven Mod
        > 1 day

        Shotgun Riven Mod
        > 1 day

        Pistol Riven Mod
        > 1 day

        Pistol Riven Mod
        > 1 day

        Pistol Riven Mod
        > 1 week

        Melee Riven Mod
        > 1 week

        Rifle Riven Mod
        > 1 week

        Shotgun Riven Mod
        > 1 week

        Pistol Riven Mod
        > 1 week

        Pistol Riven Mod
        > 1 week

        Pistol Riven Mod
        > 1 week

        Pistol Riven Mod
        > 1 week

        Rifle Riven Mod
        > 1 week

        Rifle Riven Mod
        > 1 week

        Melee Riven Mod
        > 1 week

        Shotgun Riven Mod
        > 1 week

        Rifle Riven Mod
        > 1 week