如何从我试图在字符串中抓取的网页中获取 html?

耶日·布尔佐斯卡

我编写了以下代码:

require "http/client"
require "myhtml"

puts "Give me the URL of the page to be scraped."

url = gets

html=<<-HTML
 [Here goes the html of the website to be scraped]
HTML

myhtml = Myhtml::Parser.new(html)

myhtml.nodes(:div).each do |node|
  id = node.attribute_by("id")

  if first_link = node.scope.nodes(:a).first?
    href = first_link.attribute_by("href")
    link_text = first_link.inner_text

    puts "div with id #{id} have link [#{link_text}](#{href})"
  else
    puts "div with id #{id} have no links"
  end
end

如何从我试图抓取字符串的网页中获取 html 以便我可以替换

html=<<-HTML
 [Here goes the html of the website to be scraped]
HTML

response = requests.get(url)

html = BeautifulSoup(response.text, 'html.parser')

来自以下 Python 代码:


url = input("What is the address of the web page in question?\n")

response = requests.get(url)

html = BeautifulSoup(response.text, 'html.parser')

let html = reqwest::get(url).await?.text().await?;来自以下 Rust 代码:

println!("Give me the URL of the page to be scraped."); 
 let mut url = String::new();
 io::stdin().read_line(&mut url).expect("Failed to read line");

 let html = reqwest::get(url).await?.text().await?;

分片myhtml的文档没有为我提供足够的示例来解决这个问题。可以使用Crystal 的标准库中的 HTTP 客户端来完成吗?当我更换

html=<<-HTML
 [Here goes the html of the website to be scraped]
HTML

response = HTTP::Client.get url

html = response.body

我收到以下错误:

response = HTTP::Client.get url #no overload matches 'HTTP::Client.get' with type (String | Nil)
                             ^--
Error: no overload matches 'HTTP::Client.get' with type (String | Nil)

Overloads are:
 - HTTP::Client.get(url : String | URI, headers : HTTP::Headers | ::Nil = nil, body : BodyType = nil, tls : TLSContext = nil)
 - HTTP::Client.get(url : String | URI, headers : HTTP::Headers | ::Nil = nil, body : BodyType = nil, tls : TLSContext = nil, &block)
 - HTTP::Client.get(url, headers : HTTP::Headers | ::Nil = nil, tls : TLSContext = nil, *, form : String | IO | Hash)
 - HTTP::Client.get(url, headers : HTTP::Headers | ::Nil = nil, tls : TLSContext = nil, *, form : String | IO | Hash, &block)
Couldn't find overloads for these types:
 - HTTP::Client.get(Nil)

我可以通过硬编码从网页中获取文本,例如,response = HTTP::Client.get "https://github.com/monero-project/monero/releases"但这还不够,因为我希望应用程序具有交互性。

菲利普·克拉森

你很接近,这是抱怨的类型系统。HTTP::Client.get期望一个String(或者更确切地说String | URL)。但是,在您的代码中,您的url变量也可以是nil并且是 类型String?,它是String | Nil. 如果您对 URL 进行硬编码,则它不能nil但是总是类型为String因此HTTP::Client.get调用有效。

查看函数文档get

def gets(chomp = true) : 字符串?

从此 IO 中读取一行。一行以 \n 字符结束。如果在此 IO 结束时调用,则返回 nil。

有多种方法可以解决它,但基本思想是您必须确保在进行 HTTP 调用时url不会出现nil这种情况。例如:

url = gets
if url
  # now url cannot be nil
  response = HTTP::Client.get url
  html = response.body
  puts html
end

进一步阅读:如果 var

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从 HTML 中获取特定字符串以进行网页抓取

如何从任意html字符串中获取固定的html

如何使用jQuery从字符串中获取HTML元素

如何使用Ruby将网页的HTML源代码加载到字符串中,然后解析它以获取<title>标签的内部HTML?

试图从网页中抓取 html,但没有为 Document.getClass 获取正确的类名/层次结构

如何操作 HTML 中的字符串?

在Pycharm中获取Sphinx,以将我的文档字符串包含在生成的html中

如何从HTML标记代码中获取特定字符串并检查字符串中是否存在<img>标记?

从 html 中获取 C# 中的子字符串?

如何从jquery.tmpl中的已解析html中获取子字符串

Python 中的网页抓取如何解析 html?

在包含HTML代码的字符串中获取URL

从字符串中获取 html 代码并以角度显示

从MVC 4视图获取HTML到字符串中

在 HTML 中的静态模式之间获取字符串

剥离HTML标签以在python中获取字符串

从 html 字符串中获取 ID 值

如何从Django中的网页获取HTML

如何从HTML网页中获取某些信息?

在 javascript 中构建我的 Html.Partial 字符串

如何从bash中的字符串中删除HTML?

如何从JavaScript中的字符串中剥离HTML标签?

Javascript:如何从字符串中获取所有 HTML 标签

如何从元素的 HTML 标记字符串中获取特定的属性值?

如何从没有标签的HTML字符串中获取数字?

如何使用香草JS从字符串中获取HTML元素

QRegExp 用法:如何从 html 字符串中获取图像名称?

如何从html字符串获取值?

节点JS抓取html字符串中的第一个图像