尝试在把手上渲染一些对象的变量时遇到以下问题:
我已经宣布了一个静态对象
public static class Banana
{
public static String name = "PAPI PAPI";
public static int id= 9;
public Banana( )
{
}
@Override
public String toString() {
return "Banana{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
下一步是尝试在把手上呈现我的对象变量。为此,我做了以下工作:
static void test(RoutingContext ctx)
{
HttpServerRequest req = ctx.request();
HttpServerResponse resp = ctx.response();
resp.setChunked(true);
resp.putHeader("content-type", "text/html");
ctx.put("banana", new Banana());
engine.render(ctx, "src/main/java/templates","/banana.hbs",
res -> {
if (res.succeeded()) {
ctx.response().end(res.result());
} else {
ctx.fail(res.cause());
}});
}
我的模板(banana.hbs)如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>{{banana.id}}</h1>
<h1>{{banana.name}}</h1>
</body>
问题是我的Html是空的:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1></h1>
<h1></h1>
</body>
有人可以帮忙吗? 提前谢谢,
答案 0 :(得分:0)
我认为您的问题是,您尝试在您的上下文中放置id
的非静态实例,并且通过getter无法访问name
和public class Banana {
private String name;
private Integer id;
public Banana(Integer id, String name) {
this.name = name;
this.id = id;
}
// ... toString, Getters and setters ...
static class Default() {
private static final Banana instance = new Banana(9, "PAPI PAPI");
public static Banana getInstance() {
return instance;
}
}
}
属性/ setter方法。
我认为您可以尝试更改类定义:
ctx.put("banana", Banana.Default.getInstance());
然后:
import bs4 , requests
# ask how many links he will pass
print('How many links do you want wo scrape ?')
link_numb = int(input())
# get the links
print('please enter full Ebay link ..')
links = [input() for _ in range(link_numb)]
def ebayprice(link):
res = requests.get(link)
res.raise_for_status()
txt = bs4.BeautifulSoup(res.text , 'html.parser')
csselement = txt.select('#mm-saleDscPrc')
return csselement[0].text.strip()
for link in links:
price = ebayprice(link)
print(price)