Python : 抓取游戏名称
Python : Scrape Game Names
我无法从网页上抓取游戏名称..
它返回一个空白数组。一旦名字被抓取,我希望它被写入一个新创建的文本文件。我的代码应该在下面。它远未完成,但我确定我需要一个 While 条件..
def ScrapeK10():
siteToScrape = 'http://www.kiz10.com/new-games'
print '\n[!] Requesting Kiz10..'
kizReq = requests.get(siteToScrape)
print '\n[!] Scraping Newest Games...'
kizTree - html.fromstring(kizReq.content)
kizElement = kizTree.xpath('//strong[@class="bx-caption"]/text()')
print 'Latest Games : ', kizElement, '\n'
return
我 运行 遇到的问题是我得到一个空白数组,所以我不确定我是否真的正确地抓取了网站,甚至使用了正确的 xpath?
对此还是有点陌生..不想使用 Beautiful Soup 也不想使用 Scapy..
但我的目标是抓取我提供的网页中的所有游戏名称,并将它们写入新文件..
你会使用正则表达式吗?
请注意,所有游戏名称都包含在名为 'itemsGame'.
的 JavaScript 对象中
使用正则表达式将其过滤掉,然后再次使用正则表达式拆分每一行。
应该这样做
def main():
import re
import requests
url = "http://kiz10.com/index.php?page=newgames"
raw = requests.get(url).content
match = re.search("var itemsGame = \[(.*?)\];$", raw, re.M)
for line in re.findall('\[(.*?)\]', match.group(1)):
print(line.replace("'", "").split(",")[3].strip())
或者,您可以只对来自 var itemsGame = 的字符串调用 eval()
到下一个 \n 字符。
不过,显然,eval 总是很危险,因此从未真正推荐过
我无法从网页上抓取游戏名称.. 它返回一个空白数组。一旦名字被抓取,我希望它被写入一个新创建的文本文件。我的代码应该在下面。它远未完成,但我确定我需要一个 While 条件..
def ScrapeK10():
siteToScrape = 'http://www.kiz10.com/new-games'
print '\n[!] Requesting Kiz10..'
kizReq = requests.get(siteToScrape)
print '\n[!] Scraping Newest Games...'
kizTree - html.fromstring(kizReq.content)
kizElement = kizTree.xpath('//strong[@class="bx-caption"]/text()')
print 'Latest Games : ', kizElement, '\n'
return
我 运行 遇到的问题是我得到一个空白数组,所以我不确定我是否真的正确地抓取了网站,甚至使用了正确的 xpath?
对此还是有点陌生..不想使用 Beautiful Soup 也不想使用 Scapy..
但我的目标是抓取我提供的网页中的所有游戏名称,并将它们写入新文件..
你会使用正则表达式吗? 请注意,所有游戏名称都包含在名为 'itemsGame'.
的 JavaScript 对象中使用正则表达式将其过滤掉,然后再次使用正则表达式拆分每一行。
应该这样做
def main():
import re
import requests
url = "http://kiz10.com/index.php?page=newgames"
raw = requests.get(url).content
match = re.search("var itemsGame = \[(.*?)\];$", raw, re.M)
for line in re.findall('\[(.*?)\]', match.group(1)):
print(line.replace("'", "").split(",")[3].strip())
或者,您可以只对来自 var itemsGame = 的字符串调用 eval() 到下一个 \n 字符。
不过,显然,eval 总是很危险,因此从未真正推荐过