使用Python搜寻高质量ROM的网站

假设您要从网站下载所有高质量的Super Nintendo ROM。 该站点仅具有将您带到文件本身的链接的列表。 由于这是一棵扁平树,因此可以使用URL ala运行基本的wget命令:

wget -m -np -c -w 3 -R "index.html*" "https://rom-site.blah/path/to/roms/"

但是,这将为您提供每场比赛,无论质量如何。 幸运的是,ROM发烧友使用后缀来表示rom的状态:

[a] Alternate
[p] Pirate
[b] Bad Dump     (avoid these, they may not work!)
[t] Trained
[f] Fixed
[T-] OldTranslation
[T+] NewerTranslation
[h] Hack
(-) Unknown Year
[o] Overdump
[!] Verified Good Dump
(M #) Multilanguage (# of Languages)
( ###) Checksum
(??k) ROM Size
ZZZ_ Unclassified
(Unl) Unlicensed

因此,我们只需要带有[!]后缀的代码。 您可能还希望 仅针对美国发行版 指定 [U]

当然可以使用正则表达式将其指定为wget ,但是我绝对不是wget或regex pro,因此,在尝试失败几分钟后,我放弃了并编写了一个简短的Python脚本来获取想要使用的内容美丽的汤

在编写任何代码之前,我分析了目标URL的来源,并且
可以肯定的是,该页面几乎只是一个锚定标记列表,
直接链接到ROM文件。 完善。

窥视html ,我知道我只需要从所有锚点中提取链接,但只收集包含[!]后缀的链接即可。 这可以在不到15行的Python中完成:

首先,安装beautifulsoup4:

 pip3 install beautfilsoup4

然后使用以下代码创建一个名为good_roms.py的文件:

# good_roms.py
import requests
from bs4 import BeautifulSoup

weburl = 'https://site.site/path/to/roms/'
data = requests.get(weburl)
soup = BeautifulSoup(data.text, features= 'html.parser' )

links = []
for anch in soup.find_all( 'a' ):
    if '[!]' in str(anch):
        links.append(weburl + anch.get( 'href' ))

for link in links:
    print(link)

现在,我可以运行程序并将输出重定向到文本文件:

python3 good_roms.py > rom-list.txt

现在,我有了一个包含所有好的ROM的URL的文本文件,我可以将该文件直接提供给wget ,它将使用-i输入文件开关仅下载好的文件:

wget -i rom-list.txt

而已! 确保您有足够的空间容纳所有rom,并观看它们一次堆积一堆:

--2019-01-25 21:27:02--  https://rom-site.blah/path/to/roms/YourFavoriteRom[!].bin
Reusing existing connection to [rom-site.blah]:443.
HTTP request sent, awaiting response... 200 OK
Length: 2097152 (2.0M) [application/octet-stream]
Saving to: ‘YourFavoriteRom[!].bin’

YourFavoriteRom[!].bin 100%[========================>]   2.00M   513KB/s    in 3.9s    

2019-01-25 21:27:09 (513 KB/s) - ‘YourFavoriteRom[!].bin’ saved [2097152/2097152]

FINISHED --2019-01-25 21:29:41--
Total wall clock time: 38m 47s
Downloaded: 693 files, 888M in 30m 38s (495 KB/s)

From: https://hackernoon.com/scraping-a-website-for-high-quality-roms-using-python-2x21t28ey

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值