爬虫-java更改请求头

欧阳小白 1年前 ⋅ 1762 阅读
package view;
 
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
 
import com.gargoylesoftware.htmlunit.html.DomNode;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
//设置请求头。不行
public class WebMagicService {
	public static void main(String[] args)  throws Exception{
        HttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet("http://www.vegnet.com.cn/Price/List_ar510000_p0.html?marketID=3");
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2");
        httpGet.setHeader("Accept", "Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");    
        
        httpGet.setHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");    
            
        httpGet.setHeader("Accept-Encoding", "gzip, deflate");    
            
        httpGet.setHeader("Accept-Language", "zh-cn,zh;q=0.5");    
            
        httpGet.setHeader("Connection", "keep-alive");    
            
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2");
        HttpResponse response = httpClient.execute(httpGet);
        String contents = EntityUtils.toString(response.getEntity(),"gbk");//utf-8
        Document document = Jsoup.parse(contents);
//        Elements elements = document.select("div#hza11 div.boxtxthot a");
        Elements elements = document.select(".pri_k");
    }
}

 


全部评论: 0

    我有话说:

    咨询问题

    点击这里给我发消息

    淘宝咨询

    博主承接代做业务

    真·送云服务器

    咨询博主