我有工作代码,可以
'//*[@id=all_TorontoBlueJayspitching"]/div/table/tbody/tr/th/a/text()'
从网站
使用脚本:
import requests
from lxml import html
boxScore = "CHA/CHA202206200"
url = "https://www.baseball-reference.com/boxes/" + boxScore + ".shtml"
page = requests.get(url)
tree = html.fromstring(b''.join(line for line in page.content.splitlines() if b'<!--' not in line and b'-->' not in line))
getTeams = tree.xpath('//*[@class="scorebox"]/div/div/strong/a/text()')
for team in getTeams:
team = team.replace(" ", "")
stringy = '"all_' + team + 'pitching"'
stringx = '//*[@id=' + stringy + ']/div/table/tbody/tr/th/a/text()'
tambellini = tree.xpath(stringx)
print(tambellini)
问题是我不想打印此文本,我想打印其中一条路径。这意味着我或多或少正在尝试进入
'//*[@id=all_TorontoBlueJayspitching"]/div/table/tbody/tr/th/a'
,然后在/a中值HREF(在这种情况下为href =
-一个元素,但我不知道如何作为变量访问路径本身。
I have working code that prints element
'//*[@id=all_TorontoBlueJayspitching"]/div/table/tbody/tr/th/a/text()'
From the site https://www.baseball-reference.com/boxes/CHA/CHA202206200.shtml
Using the script:
import requests
from lxml import html
boxScore = "CHA/CHA202206200"
url = "https://www.baseball-reference.com/boxes/" + boxScore + ".shtml"
page = requests.get(url)
tree = html.fromstring(b''.join(line for line in page.content.splitlines() if b'<!--' not in line and b'-->' not in line))
getTeams = tree.xpath('//*[@class="scorebox"]/div/div/strong/a/text()')
for team in getTeams:
team = team.replace(" ", "")
stringy = '"all_' + team + 'pitching"'
stringx = '//*[@id=' + stringy + ']/div/table/tbody/tr/th/a/text()'
tambellini = tree.xpath(stringx)
print(tambellini)
The problem is I do not want to print this text, I want to print one of the paths. Meaning I more or less am trying to get to
'//*[@id=all_TorontoBlueJayspitching"]/div/table/tbody/tr/th/a'
And then that value href in /a (which in this case is href=-"/players/b/berrijo01.shtml"
Any guidance here would be helpful. I know how to successfully print an element, but I don't know how to access the path itself as a variable. Thank you.
发布评论
评论(1)
将字符串X更改为
应该输出
Change the stringx to
This should output