【发布时间】:2020-08-14 03:22:23
【问题描述】:
我正在 Python 库 discord.py 中编写 Discord 机器人。
我不需要帮助,但需要从网站上抓取一些信息。
@commands.command(aliases=["rubyuserinfo"])
async def rubyinfo(self, ctx, input):
HEADERS = {
'User-Agent' : 'Magic Browser'
}
url = f'https://rubyrealms.com/user/{input}/'
async with aiohttp.request("GET", url, headers=HEADERS) as response:
if response.status == 200:
print("Site is working!")
content = await response.text()
soup = BeautifulSoup(content, "html.parser")
page = requests.get(url)
tree = html.fromstring(page.content)
stuff = tree.xpath('/html/body/div[4]/div/div[3]/div[3]/div/div[2]/div[1]/div[2]/div/p')
print(stuff)
else:
print(f"The request was invalid\nStatus code: {response.status}")
我正在寻找的网站是“https://rubyrealms.com/user/{input}/”,在运行 h!rubyinfo USERNAME 时会给出输入,将链接更改为 https://rubyrealms.com/user/username/。
在网站上我想得到的是他们的 BIO,它的 XPATH 为
"//*[@id="content-wrap"]/div[3]/div[3]/div/div[2]/div[1]/div[2]/div/p"
元素在哪里:
<p class="margin-none font-color">
Hey! My name is KOMKO190, you maybe know me from the forums or discord. I am a programmer, I know a bit of JavaScript, small portion of C++, Python and html/css. Mostly python. My user ID is 7364. ||| 5th owner of Space Helmet :) </p>
对我将如何刮它有任何帮助吗?我的机器人给出的唯一响应是“[]”
【问题讨论】:
标签: python xpath web-scraping beautifulsoup lxml