【问题标题】:Why am I getting no output?为什么我没有输出?
【发布时间】:2019-12-21 15:38:33
【问题描述】:

我真的有一个非常简单的问题 - 为什么我没有输出?这是网站:https://riven.market/list/PC/Veiled。我认为问题是类名中的空格,但结果是自然的,不应该引起任何问题。如果您有任何问题,请在 cmets 中告诉我

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(" ", ""))

【问题讨论】:

  • 没有<div>riven-list 的标签
  • 那个页面没有这样的div。不要使用检查器来抓取项目,因为它会误导你。仅使用“查看源代码”。
  • 不知怎的还是不行
  • @Stasiek 它不起作用,因为如果您查看页面源代码,则没有带有riven-list<div> 标签。就像Selcuk所说的,不要使用inspector对象来获取元素的ID,而是查看页面源来抓取它。

标签: python beautifulsoup python-requests


【解决方案1】:

这是我的解决方法,即查找查询将请求发送到的实际 URL。你可以通过rightclick-> inspect element -> network -> Find the get request找到它

import requests
from bs4 import BeautifulSoup

# instead of sending to this
main = requests.get("https://riven.market/list/PC/Veiled")

ajax_url = "https://riven.market/_modules/riven/showrivens.php?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=1565851905857"
re = requests.get(ajax_url)
c = re.content

soup = BeautifulSoup(c, "html.parser")
all_divs = soup.find_all("div", class_ ="attribute weapon" )
for item in all_divs:
        print(item.text)

输出


        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

【讨论】:

    【解决方案2】:

    不确定您想要的输出到底是什么,但您需要对请求 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-14
      • 1970-01-01
      相关资源
      最近更新 更多