[英]How to add custom HTML to a Mechanize page object
我想知道是否可以向Mechanize頁面對象添加自定義HTML代碼。 目標是通過將由javascript代碼生成的HTML添加到機械化頁面對象(通過agent.get(uri)方法進行抓取),並使機械化代理單擊提交表單,就好像它避免生成表單的javascript代碼一樣。真的在那里。 可以直接在頁面上創建表格,而無需使用jpeg進行外部調用,因此應該可以使用它。 我不能使用Selenium和類似物,我需要堅持使用Mechanize和Nokogiri。 任何幫助,甚至方向將不勝感激!
查看Mechanize文檔中的“ Scraping Data ”。 因為您可以訪問和搜索Nokogiri文檔,所以可以對其進行修改。
使用Mechanize內部使用的Nokogiri可以輕松地修改文檔:
require 'mechanize'
agent = Mechanize.new
page = agent.get('http://www.example.org')
doc = page.parser
first_p = doc.at('p')
first_p.to_html # => "<p>This domain is established to be used for illustrative examples in documents. You may use this\n domain in examples without prior coordination or asking for permission.</p>"
first_p.children = '
<form action="action_page.php">
First name:<br>
<input type="text" name="firstname" value="First name"><br>
Last name:<br>
<input type="text" name="lastname" value="Last name"><br><br>
<input type="submit" value="Submit">
</form>'
first_p.to_html # => "<p>\n <form action=\"action_page.php\">\n First name:<br>\n <input type=\"text\" name=\"firstname\" value=\"First name\"><br>\n Last name:<br>\n <input type=\"text\" name=\"lastname\" value=\"Last name\"><br><br>\n <input type=\"submit\" value=\"Submit\">\n </form></p>"
在父級中向上一級查找:
page.parser.at('p').parent.to_html # => "<div>\n <h1>Example Domain</h1>\n <p>\n <form action=\"action_page.php\">\n First name:<br>\n <input type=\"text\" name=\"firstname\" value=\"First name\"><br>\n Last name:<br>\n <input type=\"text\" name=\"lastname\" value=\"Last name\"><br><br>\n <input type=\"submit\" value=\"Submit\">\n </form></p>\n <p><a href=\"http://www.iana.org/domains/example\">More information...</a></p>\n</div>"
您是否可以將Mechanize與修改后的HTML結合使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.