如何通过网络抓取所有击球手的名字?
How to web scrape all of the batters names?
我想抓取 2018 年所有 MLB 击球手的统计数据。这是我目前的代码:
#import modules
from urllib.request import urlopen
from lxml import html
#fetch url/html
response = urlopen("https://www.baseball-reference.com/leagues/MLB/2018-standard-batting.shtml")
content = response.read()
tree = html.fromstring( content )
#parse data
comment_html = tree.xpath('//comment()[contains(., "players_standard_batting")]')[0]
comment_html = str(comment_html).replace("-->", "")
comment_html = comment_html.replace("<!--", "")
tree = html.fromstring( comment_html )
for batter_row in tree.xpath('//table[@id="players_standard_batting"]/tbody/tr[contains(@class, "full_table")]'):
csk = batter_row.xpath('./td[@data-stat="player"]/@csk')[0]
当我刮掉所有击球手时,每个名字都附有 0.01。我尝试使用以下代码删除附加号码:
bat_data = [csk]
string = '0.01'
result = []
for x in bat_data :
if string in x:
substring = x.replace(string,'')
if substring != "":
result.append(substring)
else:
result.append(x)
print(result)
此代码删除了号码,但是只打印了姓氏:
输出:
['Zunino, Mike']
此外,名称周围有括号和引号。名字也是倒序的。
1) 如何打印所有击球手的名字?
2) 如何去掉引号和括号?
3) 我可以颠倒名字的顺序以便先打印名字然后打印姓氏吗?
我希望的最终输出是所有击球手的名字,如下所示:Mike Zunino。
我是这个网站的新手...我也是 scraping/coding 的新手,如果能得到任何帮助,我将不胜感激! =)
1) 打印所有击球手名字
print(result)
这将打印结果对象中的所有内容。如果它没有打印出您期望的内容,则表明存在其他问题。
2) 删除引号
括号是因为它是一个数组对象。试试这个...
print(result[0])
这将告诉解释器在 0 索引处打印结果。
3) 名字倒序
尝试
name = result[0].split(“ “).reverse()[::-1]
你只得到最后一个击球手,因为你在第一个循环中每次都覆盖了 csk 的值。首先初始化空列表bat_data,然后将每个batter添加到其中。
bat_data= []
for batter_row in blah:
csk = blah
bat_data.append(csk)
这将为您提供所有击球手的列表,['Abreu,Jose0.01', 'Acuna,Ronald0.01', 'Adam,Jason0.01', ...]
然后遍历此列表,但您不必检查 string
是否在名称中。只需执行 x.replace('0.01', '')
然后检查字符串是否为空。
颠倒名字的顺序
substring = substring.split(',')
substring.reverse()
nn = " ".join(substring)
然后将 nn 附加到结果。
您得到的是引号和括号,因为您正在打印列表。而是遍历列表并打印每个项目。
假设您 bat_data 正确地编辑了您的代码:
for x in bat_data :
substring = x.replace(string,'')
if substring != "":
substring = substring.split(',')
substring.reverse()
substring = ' '.join(substring)
result.append(substring)
for x in result:
print(x)
你可以用不同的方式来做同样的事情。这是一种不需要 post 处理的方法。你得到你想要的名字:
from urllib.request import urlopen
from lxml.html import fromstring
url = "https://www.baseball-reference.com/leagues/MLB/2018-standard-batting.shtml"
content = str(urlopen(url).read())
comment = content.replace("-->","").replace("<!--","")
tree = fromstring(comment)
for batter_row in tree.xpath('//table[contains(@class,"stats_table")]//tr[contains(@class,"full_table")]'):
csk = batter_row.xpath('.//td[@data-stat="player"]/a')[0].text
print(csk)
您可能会得到这样的输出:
Jose Abreu
Ronald Acuna
Jason Adam
Willy Adames
Austin L. Adams
我想抓取 2018 年所有 MLB 击球手的统计数据。这是我目前的代码:
#import modules
from urllib.request import urlopen
from lxml import html
#fetch url/html
response = urlopen("https://www.baseball-reference.com/leagues/MLB/2018-standard-batting.shtml")
content = response.read()
tree = html.fromstring( content )
#parse data
comment_html = tree.xpath('//comment()[contains(., "players_standard_batting")]')[0]
comment_html = str(comment_html).replace("-->", "")
comment_html = comment_html.replace("<!--", "")
tree = html.fromstring( comment_html )
for batter_row in tree.xpath('//table[@id="players_standard_batting"]/tbody/tr[contains(@class, "full_table")]'):
csk = batter_row.xpath('./td[@data-stat="player"]/@csk')[0]
当我刮掉所有击球手时,每个名字都附有 0.01。我尝试使用以下代码删除附加号码:
bat_data = [csk]
string = '0.01'
result = []
for x in bat_data :
if string in x:
substring = x.replace(string,'')
if substring != "":
result.append(substring)
else:
result.append(x)
print(result)
此代码删除了号码,但是只打印了姓氏:
输出:
['Zunino, Mike']
此外,名称周围有括号和引号。名字也是倒序的。
1) 如何打印所有击球手的名字?
2) 如何去掉引号和括号?
3) 我可以颠倒名字的顺序以便先打印名字然后打印姓氏吗?
我希望的最终输出是所有击球手的名字,如下所示:Mike Zunino。
我是这个网站的新手...我也是 scraping/coding 的新手,如果能得到任何帮助,我将不胜感激! =)
1) 打印所有击球手名字
print(result)
这将打印结果对象中的所有内容。如果它没有打印出您期望的内容,则表明存在其他问题。
2) 删除引号 括号是因为它是一个数组对象。试试这个...
print(result[0])
这将告诉解释器在 0 索引处打印结果。
3) 名字倒序
尝试
name = result[0].split(“ “).reverse()[::-1]
你只得到最后一个击球手,因为你在第一个循环中每次都覆盖了 csk 的值。首先初始化空列表bat_data,然后将每个batter添加到其中。
bat_data= []
for batter_row in blah:
csk = blah
bat_data.append(csk)
这将为您提供所有击球手的列表,['Abreu,Jose0.01', 'Acuna,Ronald0.01', 'Adam,Jason0.01', ...]
然后遍历此列表,但您不必检查 string
是否在名称中。只需执行 x.replace('0.01', '')
然后检查字符串是否为空。
颠倒名字的顺序
substring = substring.split(',')
substring.reverse()
nn = " ".join(substring)
然后将 nn 附加到结果。
您得到的是引号和括号,因为您正在打印列表。而是遍历列表并打印每个项目。
假设您 bat_data 正确地编辑了您的代码:
for x in bat_data :
substring = x.replace(string,'')
if substring != "":
substring = substring.split(',')
substring.reverse()
substring = ' '.join(substring)
result.append(substring)
for x in result:
print(x)
你可以用不同的方式来做同样的事情。这是一种不需要 post 处理的方法。你得到你想要的名字:
from urllib.request import urlopen
from lxml.html import fromstring
url = "https://www.baseball-reference.com/leagues/MLB/2018-standard-batting.shtml"
content = str(urlopen(url).read())
comment = content.replace("-->","").replace("<!--","")
tree = fromstring(comment)
for batter_row in tree.xpath('//table[contains(@class,"stats_table")]//tr[contains(@class,"full_table")]'):
csk = batter_row.xpath('.//td[@data-stat="player"]/a')[0].text
print(csk)
您可能会得到这样的输出:
Jose Abreu
Ronald Acuna
Jason Adam
Willy Adames
Austin L. Adams