java503错误是什么_java - 使用Jsoup访问HTML但收到错误代码503

我一直在尝试访问KissAnime,但是我一直收到此错误:

org.jsoup.HttpStatusException:提取URL时发生HTTP错误。状态= 503

当我尝试另一个URL例如。 https://www.google.com/,它可以完美运行。

这是我的asynctask代码:@Override

protected Void doInBackground(Void... params) {

try {

// Connect to the web site

Document document = Jsoup.connect("https://kissanime.to/AnimeList/")

.ignoreContentType(true)

.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1")

.referrer("http://www.google.com")

.timeout(12000)

.followRedirects(true)

.get();

// Get the html document title

title = document.title();

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

任何想法如何解决这个问题?

最佳答案

要摆脱503,下面的代码将很有用。public static void main(String[] args) throws IOException {

//This will get you out of Http 503

Document document = Jsoup.connect("https://kissanime.to/AnimeList/")

.userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:49.0) Gecko/20100101 Firefox/49.0").ignoreHttpErrors(true).followRedirects(true).timeout(100000).ignoreContentType(true).get();

System.out.println(document.body());

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Javajsoup库从京东网站爬取商品图片的代码示例: ```java import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.ArrayList; import java.util.List; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; public class JdImageCrawler { public static void main(String[] args) throws IOException { String url = "https://search.jd.com/Search?keyword=手机"; List<String> imageUrls = getJdImageUrls(url); downloadImages(imageUrls); } /** * 从京东搜索结果页面获取商品图片链接 * @param url 京东搜索结果页面的URL * @return 商品图片链接列表 * @throws IOException */ public static List<String> getJdImageUrls(String url) throws IOException { List<String> imageUrls = new ArrayList<>(); Document doc = Jsoup.connect(url).get(); Elements elements = doc.select(".gl-item .p-img img"); for (Element element : elements) { String imageUrl = element.attr("data-lazy-img"); if (imageUrl == null || imageUrl.isEmpty()) { imageUrl = element.attr("src"); } imageUrls.add(imageUrl.replace("/n9/", "/n1/")); } return imageUrls; } /** * 下载图片到本地 * @param imageUrls 商品图片链接列表 * @throws IOException */ public static void downloadImages(List<String> imageUrls) throws IOException { for (String imageUrl : imageUrls) { URL url = new URL(imageUrl); InputStream is = url.openStream(); String fileName = imageUrl.substring(imageUrl.lastIndexOf("/") + 1); byte[] bytes = new byte[1024]; int len; try (FileOutputStream fos = new FileOutputStream(fileName)) { while ((len = is.read(bytes)) != -1) { fos.write(bytes, 0, len); } } } } } ``` 这个代码示例使用jsoup库从京东搜索结果页面获取商品图片链接,并使用Java标准库下载这些图片到本地。注意,这个示例代码并没有处理异常情况,实际应用中需要加入更多的错误处理和异常处理代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值