import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
public class crawlHtml {
public static void main(String[] args){
}
//爬取网页数据
public static String pickData(String url){
try{
CloseableHttpClient httpClient=HttpClientBuilder.create().build();
HttpGet httpGet=new HttpGet(url);
CloseableHttpResponse response=httpClient.execute(httpGet);//这一行报错,错误我放在了最下面
}catch(Exception e){
e.printStackTrace();
}
}
}
错误:
Multiple markers at this line
- The type org.apache.http.HttpResponse cannot be resolved. It is indirectly referenced from required .class files
- The type org.apache.http.HttpHost cannot be resolved. It is indirectly referenced from required .class files
- The type org.apache.http.protocol.HttpContext cannot be resolved. It is indirectly referenced from
required .class files
我的Eclipse和JDK1.8,运行其他项目是可以的。应该没有不兼容的问题
只是想写一个简单的爬虫程序,被卡在这里好久了,麻烦各位大神帮忙看看。谢谢!
试试
import org.apache.http.HttpResponse;
不行啊
爬虫还是用python写比较好,一看就会.给你个小demo,给点园豆
from urllib import request from bs4 import BeautifulSoup # Beautiful Soup是一个可以从HTML或XML文件中提取结构化数据的Python库 # 构造头文件,模拟浏览器访问 url = "http://www.jianshu.com" headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'} page = request.Request(url, headers=headers) page_info = request.urlopen(page).read().decode('utf-8') # 打开Url,获取HttpResponse返回对象并读取其ResposneBody # 将获取到的内容转换成BeautifulSoup格式,并将html.parser作为解析器 soup = BeautifulSoup(page_info, 'html.parser') # 以格式化的形式打印html print(soup.prettify()) titles = soup.find_all('a', 'title') # 查找所有a标签中class='title'的语句 # 打印查找到的每一个a标签的string和文章链接 for title in titles: print(title.string) print("http://www.jianshu.com" + title.get('href')) # open()是读写文件的函数,with语句会自动close()已打开文件 with open(r"F:\PhyWorkSpeace\aaa.txt", "w") as file: # 在磁盘以只写的方式打开/创建一个名为 aaa 的txt文件 for title in titles: file.write(title.string + '\n') file.write("http://www.jianshu.com" + title.get('href') + '\n\n')
我知道python写爬虫方便些,可是我问的是java啊 老哥