Pse*_*erd 15 web-scraping julia
我想从这个站点中提取大学及其网站的名称到列表中。
在 Python 中,我使用 BeautifulSoup v4 做到了:
import requests
from bs4 import BeautifulSoup
import pandas as pd
page = requests.get('https://thebestschools.org/features/best-computer-science-programs-in-the-world/')
content = BeautifulSoup(page.text, 'html.parser')
college_name = []
college_link = []
college_name_list = content.find_all('h3',class_='college')
for college in college_name_list:
if college.find('a'):
college_name.append(college.find('a').text)
college_link.append(college.find('a')['href'])
Run Code Online (Sandbox Code Playgroud)
我真的很喜欢用 Julia 编程,因为它与 Python 非常相似,我想知道我是否也可以在 Julia 中进行网络抓取。任何帮助,将不胜感激。
Lyn*_*ite 11
你的 python 代码不太好用。我猜这个网站最近更新了。因为据我所知,他们已经删除了链接。这是一个使用Gumbo.jl和Cascadia.jl的类似示例。
我正在使用内置download
命令下载网页。它将它写入临时文件中的磁盘,然后我将其读入字符串。使用HTTP.jl可能更干净,它可以直接将其读入字符串。但对于这个简单的例子,它很好
using Gumbo
using Cascadia
url = "https://thebestschools.org/features/best-computer-science-programs-in-the-world/"
page = parsehtml(read(download(url), String))
college_name = String[]
college_location = String[]
sections = eachmatch(sel"section", page.root)
for section in sections
maybe_col_heading = eachmatch(sel"h3.college", section)
if length(maybe_col_heading) == 0
continue
end
col_heading = first(maybe_col_heading)
name = strip(text(last(col_heading.children)))
push!(college_name, name)
loc = first(eachmatch(sel".school-location", section))
push!(college_location, text(loc[1]))
end
[college_name college_location]
Run Code Online (Sandbox Code Playgroud)
julia> [college_name college_location]
51×2 Array{String,2}:
"Massachusetts Institute of Technology (MIT)" "Cambridge, Massachusetts"
"Massachusetts Institute of Technology (MIT)" "Cambridge, Massachusetts"
"Stanford University" "Stanford, California"
"Carnegie Mellon University" "Pittsburgh, Pennsylvania"
?
"Shanghai Jiao Tong University" "Shanghai, China"
"Lomonosov Moscow State University" "Moscow, Russia"
"City University of Hong Kong" "Hong Kong"
Run Code Online (Sandbox Code Playgroud)
似乎它两次列出了麻省理工学院。可能我演示中的过滤代码不是很安静。但是 :shrug: 我听说麻省理工学院是一所很棒的大学。Julia 是在那里发明的 :joy:
是的。
为了抓取网页,Julia 有三个库:
requests
库),我从你的个人资料中看到你很年轻(16 岁),你的 python 实现也是正确的。
因此,我建议您尝试使用这三个库进行网络抓取任务,以更好地了解它们的工作原理。
您希望做任务,不幸的是,目前还不能与卡斯凯迪亚完成,因为h3
是在<span>
当前未在Cascadia.jl的实施SelectorType
来源