gpt4 book ai didi

web-scraping - 从Wikipedia抓取数据

转载 作者:行者123 更新时间:2023-12-03 15:12:46 24 4
gpt4 key购买 nike

我正在尝试查找或构建一个网络爬虫,该爬虫能够通过并找到美国的每个州/国家公园及其GPS坐标和陆地区域。我研究了Scrapy之类的框架,然后看到一些专门用于Wikipedia的站点,例如http://wiki.dbpedia.org/About。这两个中的任何一个是否有任何特定的优势,或者将这些信息加载到在线数据库中哪个效果更好?

最佳答案

假设您要解析this Wikipedia page之类的页面。下面的代码应该工作。

var doc = new HtmlDocument(); 
doc = .. //Load the document here. See doc.Load(..), doc.LoadHtml(..), etc.

//We get all the rows from the table (except the header)
var rows = doc.DocumentNode.SelectNodes("//table[contains(@class, 'sortable')]//tr").Skip(1);
foreach (var row in rows) {
var name = HttpUtility.HtmlDecode(row.SelectSingleNode("./*[1]/a[@href and @title]").InnerText);
var loc = HttpUtility.HtmlDecode(row.SelectSingleNode(".//span[@class='geo-dec']").InnerText);
var areaNodes = row.SelectSingleNode("./*[5]").ChildNodes.Skip(1);
string area = "";
foreach (var a in areaNodes) {
area += HttpUtility.HtmlDecode(a.InnerText);
}

Console.WriteLine("{0,-30} {1,-20} {2,-10}", name, loc, area);
}

我对其进行了测试,并产生以下输出:
Acadia                         44.35A°N 68.21A°W    47,389.67 acres (191.8 km2)
American Samoa 14.25A°S 170.68A°W 9,000.00 acres (36.4 km2)
Arches 38.68A°N 109.57A°W 76,518.98 acres (309.7 km2)
Badlands 43.75A°N 102.50A°W 242,755.94 acres (982.4 km2)
Big Bend 29.25A°N 103.25A°W 801,163.21 acres (3,242.2 km2)
Biscayne 25.65A°N 80.08A°W 172,924.07 acres (699.8 km2)
Black Canyon of the Gunnison 38.57A°N 107.72A°W 32,950.03 acres (133.3 km2)
Bryce Canyon 37.57A°N 112.18A°W 35,835.08 acres (145.0 km2)
Canyonlands 38.2A°N 109.93A°W 337,597.83 acres (1,366.2 km2)
Capitol Reef 38.20A°N 111.17A°W 241,904.26 acres (979.0 km2)
Carlsbad Caverns 32.17A°N 104.44A°W 46,766.45 acres (189.3 km2)
Channel Islands 34.01A°N 119.42A°W 249,561.00 acres (1,009.9 km2)
Congaree 33.78A°N 80.78A°W 26,545.86 acres (107.4 km2)
Crater Lake 42.94A°N 122.1A°W 183,224.05 acres (741.5 km2)
Cuyahoga Valley 41.24A°N 81.55A°W 32,860.73 acres (133.0 km2)
Death Valley 36.24A°N 116.82A°W 3,372,401.96 acres (13,647.6 km2)
Denali 63.33A°N 150.50A°W 4,740,911.72 acres (19,185.8 km2)
Dry Tortugas 24.63A°N 82.87A°W 64,701.22 acres (261.8 km2)
Everglades 25.32A°N 80.93A°W 1,508,537.90 acres (6,104.8 km2)
Gates of the Arctic 67.78A°N 153.30A°W 7,523,897.74 acres (30,448.1 km2)
Glacier 48.80A°N 114.00A°W 1,013,572.41 acres (4,101.8 km2)
(...)

我认为这是一个开始。如果某些页面失败,则必须查看布局是否发生更改等。

当然,您还必须找到一种获取所有要解析的链接的方法。

一件重要的事情:您知道是否允许刮取Wikipedia吗?我不知道,但是您应该在执行操作之前先查看它是否... ;)

关于web-scraping - 从Wikipedia抓取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12415214/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com