通过 LXML 通过 XPATH 查找元素 - Python
Find element by XPATH via LXML - Python
我在使用 LXML 抓取一些 Web 数据时遇到了一些问题。
我想使用 BeautifulSoup 从网站上抓取一些东西,所以我决定使用 LXML。我写了一些代码并让 Discord Bot 访问该网站。现在唯一剩下的就是编写代码来查找这些元素。
这是我的代码,将不胜感激。
@tasks.loop(seconds = 10)
async def exchangeRate(self):
print("Loop Starting!")
HEADERS = {
'User-Agent' : "Magic Browser"
}
url = 'https://rubyrealms.com/economy/bank'
async with aiohttp.request("GET", url, headers=HEADERS) as response:
if response.status == 200:
#Scrape page content into one variable
content = await response.text()
#Initialize soup
soup = BeautifulSoup(content, "html.parser")
#Request access to site
page = requests.get(url)
#Declaring "tree" - Used to scrape by XPATH
tree = html.fromstring(page.content)
stuff = tree.xpath('//*[@id="content-wrap"]/div[3]/div[3]/div[2]/div[1]/div[2]/div[1]/div[2]/div[2]/h4')
print(stuff)
else:
print(f"The request was invalid\nStatus code: {response.status}")
这是我的 Discord.Py ReWrite 任务循环,基本上每 10 秒它就会访问该站点。如图所示,以下代码有效,除此之外:
stuff = tree.xpath('//*[@id="content-wrap"]/div[3]/div[3]/div[2]/div[1]/div[2]/div[1]/div[2]/div[2]/h4')
print(stuff)
它唯一打印的是“Loop Starting!”从循环的开始。使用上面的代码(长代码)我打印出这个:
Bot is ready for duty!
Exchange Cog is ready!
Waiting for loop!
Loop Starting!
[]
我要显示的是:
Bot is ready for duty!
Exchange Cog is ready!
Waiting for loop!
Loop Starting!
243
(这个数字每天都在变化,所以我不能只用一次。)
如果有人知道我将如何解决这个问题,请帮忙。提前谢谢你。
tree
有 7 个 <h4>
标签符合您评论中的描述。如果我没理解错的话,为了得到全部 7 个,你可以使用这个:
stuff = tree.xpath('//h4[@data-toggle="tooltip"]')
for s in stuff:
print(s.text)
输出为:
246
2
7
16
1
1
1
如果您提前知道您的目标号码(例如 tree
中的 246
)总是第一个,您甚至可以将其缩短为:
stuff = tree.xpath('//h4[@data-toggle="tooltip"]')[0]
print(stuff.text)
输出将是:
246
我在使用 LXML 抓取一些 Web 数据时遇到了一些问题。 我想使用 BeautifulSoup 从网站上抓取一些东西,所以我决定使用 LXML。我写了一些代码并让 Discord Bot 访问该网站。现在唯一剩下的就是编写代码来查找这些元素。 这是我的代码,将不胜感激。
@tasks.loop(seconds = 10)
async def exchangeRate(self):
print("Loop Starting!")
HEADERS = {
'User-Agent' : "Magic Browser"
}
url = 'https://rubyrealms.com/economy/bank'
async with aiohttp.request("GET", url, headers=HEADERS) as response:
if response.status == 200:
#Scrape page content into one variable
content = await response.text()
#Initialize soup
soup = BeautifulSoup(content, "html.parser")
#Request access to site
page = requests.get(url)
#Declaring "tree" - Used to scrape by XPATH
tree = html.fromstring(page.content)
stuff = tree.xpath('//*[@id="content-wrap"]/div[3]/div[3]/div[2]/div[1]/div[2]/div[1]/div[2]/div[2]/h4')
print(stuff)
else:
print(f"The request was invalid\nStatus code: {response.status}")
这是我的 Discord.Py ReWrite 任务循环,基本上每 10 秒它就会访问该站点。如图所示,以下代码有效,除此之外:
stuff = tree.xpath('//*[@id="content-wrap"]/div[3]/div[3]/div[2]/div[1]/div[2]/div[1]/div[2]/div[2]/h4')
print(stuff)
它唯一打印的是“Loop Starting!”从循环的开始。使用上面的代码(长代码)我打印出这个:
Bot is ready for duty!
Exchange Cog is ready!
Waiting for loop!
Loop Starting!
[]
我要显示的是:
Bot is ready for duty!
Exchange Cog is ready!
Waiting for loop!
Loop Starting!
243
(这个数字每天都在变化,所以我不能只用一次。)
如果有人知道我将如何解决这个问题,请帮忙。提前谢谢你。
tree
有 7 个 <h4>
标签符合您评论中的描述。如果我没理解错的话,为了得到全部 7 个,你可以使用这个:
stuff = tree.xpath('//h4[@data-toggle="tooltip"]')
for s in stuff:
print(s.text)
输出为:
246
2
7
16
1
1
1
如果您提前知道您的目标号码(例如 tree
中的 246
)总是第一个,您甚至可以将其缩短为:
stuff = tree.xpath('//h4[@data-toggle="tooltip"]')[0]
print(stuff.text)
输出将是:
246