2017-08-01 1957 views
1

這是我最後一次求救幫助我正在嘗試使用我的不和機器人進行一些很酷的嵌入,唯一的問題是我無法從網站獲取img到任何人都可以幫忙嗎?大部分情況下,這是其他人告訴我使用的代碼,這裏找到的代碼不起作用。使用beautifulsoup從img標籤獲取src

async def events(self, ctx): 
    """Top GTAO bounses going on right now!""" 

    if ctx.message.server.me.bot: 
     try: 
      await self.bot.delete_message(ctx.message) 
     except: 
      await self.bot.send_message(ctx.message.author, 'Could not delete your message on ' + ctx.message.server.name) 

    url = "https://socialclub.rockstargames.com/" 

    async with aiohttp.get(url) as response: 
     soupObject = BeautifulSoup(await response.text(), "html.parser") 

    try: 
     rm = "[Read More](https://socialclub.rockstargames.com/events)" 
     img = "https://i.imgur.com/0Gu4sSK.png" 
     avi = "https://i.imgur.com/s5O1yD2.png" 
     bonus1 = soupObject.find(class_='bonuses').find('ul').get_text() 
     evpic = soupObject.find(class_='eventThumb').find('img').get('src') 
     # EMBED 
     data = discord.Embed(title='GTA Online Bonuses', description='The Current GTA Online Bonuses', colour=0xE4BA22) 
     data.set_author(name='Rockstar Games', icon_url=avi) 
     data.add_field(name="This week: \n", value=bonus1) 
     data.add_field(name="--------", value=rm) 
     data.set_image(url=evpic) 
     data.set_thumbnail(url=img) 
     a`enter code here`wait self.bot.say(embed=data) 


    except discord.HTTPException: 
     await self.bot.say("I need the `Embed links` permission to send this OR error") 
+0

這哪個部分不起作用? –

+0

嘗試'evpic = soupObject.find('img',{'class':'eventThumb'})['src']' –

+0

我得到了TypeError:'NoneType'對象不可訂閱 –

回答

0

檢查網站的Rockstar不使用src標籤在他們的圖像,因爲它是由一些內部處理JS

>>> soup.find(attrs={'class':'eventThumb'}) 
<div class="eventThumb"> 
<img class="lazyload" data-src="https://prod.cloud.rockstargames.com/global/Events/20449/829a53e7-d14e-4de8-a17b-ccb06becfed6.jpg"/> 
</div> 
>>> _.img 
<img class="lazyload" data-src="https://prod.cloud.rockstargames.com/global/Events/20449/829a53e7-d14e-4de8-a17b-ccb06becfed6.jpg"/> 
>>> _.get('data-src') 
'https://prod.cloud.rockstargames.com/global/Events/20449/829a53e7-d14e-4de8-a17b-ccb06becfed6.jpg' 

所以要解決,你需要你的.get('src')更改爲.get('data-src')

+0

工程!謝謝!! –