A. 用java編寫網路爬蟲,用來爬網路音樂資源,再返回java頁面顯示該怎麼實現
下面是源代碼,希望可以幫到你~~
package com.ly.mainprocess;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.Consts;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class Test1 {
public static void main(String[] args){
Test1 test1 = new Test1();
System.out.println(test1.process("******","******"));
}
@SuppressWarnings("deprecation")
public boolean process(String username,String password) {
boolean ret=false;
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget;
HttpResponse response;
HttpEntity entity;
List<Cookie> cookies;
//組建登錄的post包
HttpPost httppost = new HttpPost("http://login.hi.mop.com/Login.do"); // 用戶登錄
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("nickname", username));
nvps.add(new BasicNameValuePair("password", password));
nvps.add(new BasicNameValuePair("origURL", "http://hi.mop.com/SysHome.do"));
nvps.add(new BasicNameValuePair("loginregFrom", "index"));
nvps.add(new BasicNameValuePair("ss", "10101"));
httppost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
httppost.addHeader("Referer", "http://hi.mop.com/SysHome.do");
httppost.addHeader("Connection", "keep-alive");
httppost.addHeader("Content-Type", "application/x-www-form-urlencoded");
httppost.addHeader("Accept-Language", "zh-CN,zh;q=0.8");
httppost.addHeader("Origin", "http://hi.mop.com");
httppost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36");
response = httpclient.execute(httppost);
entity = response.getEntity();
// System.out.println("Login form get: " + response.getStatusLine());
EntityUtils.consume(entity);
// System.out.println("Post logon cookies:");
cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
// System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
// System.out.println("- " + cookies.get(i).toString());
}
}
//進行頁面跳轉
String url = ""; // 頁面跳轉
Header locationHeader = response.getFirstHeader("Location");
// System.out.println(locationHeader.getValue());
if (locationHeader != null) {
url = locationHeader.getValue(); // 得到跳轉href
HttpGet httpget1 = new HttpGet(url);
response = httpclient.execute(httpget1);
// 登陸成功。。。hoho
}
entity = response.getEntity();
// System.out.println(response.getStatusLine());
if (entity != null) {
// System.out.println("Response content length: " + entity.getContentLength());
}
// 顯示結果
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null) {
// System.out.println(line);
}
//自動打卡
// 訪問網站的子網頁。
HttpPost httppost1 = new HttpPost("http://home.hi.mop.com/ajaxGetContinusLoginAward.do"); // 設置個人信息頁面
httppost1.addHeader("Content-Type", "text/plain;charset=UTF-8");
httppost1.addHeader("Accept", "text/plain, */*");
httppost1.addHeader("X-Requested-With", "XMLHttpRequest");
httppost1.addHeader("Referer", "http://home.hi.mop.com/Home.do");
response = httpclient.execute(httppost1);
entity = response.getEntity();
// System.out.println(response.getStatusLine());
if(response.getStatusLine().toString().indexOf("HTTP/1.1 200 OK")>=0){
ret = true;
}
if (entity != null) {
// System.out.println("Response content length: " + entity.getContentLength());
}
// 顯示結果
reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
} finally {
httpclient.getConnectionManager().shutdown();
}
return ret;
}
}
B. 能用java寫出一個爬取音頻和視頻的爬蟲嗎
這個肯定是可以的,不過我只做過爬取文本和圖片的。沒做過爬視頻和音頻的,不過就我所知是可以的
C. python爬蟲爬取的數據可以做什麼
爬蟲的概念是,爬取網上能看到的數據,也就是只要網上存在的,通過瀏覽器可以看到的數據。爬蟲都可以爬取。爬蟲爬取的原理就是偽裝成瀏覽器,然後進行爬取操作
哪些數據你需要你就可以爬取。比如爬取公司競爭對手的商業數據,爬取電影,音樂,圖片等等的。只要你希望得到的,前提瀏覽器可以訪問的都可以爬取
D. 如何使用Python爬取網易雲音樂歌單內的歌曲
1首先在瀏覽器在打開網易雲音樂的網頁版,並點進一個歌單。
2.在瀏覽器的開發者工具審查該頁面的元素(一般按f12可以彈出該工具),選擇Network,之後選擇doc可以簡便地找到我們需要的元素。
註:以下兩步第3,第4步主要是為了防止網站的反爬,經過這兩步後,基本可以避開網站的反爬,所以如果你爬取的網站沒有反爬的話,可以跳過這兩步。
3.查看網頁的請求方式--get請求
4.查看header
5.在Response中找到我們所需要的元素所在的標簽位置(如:我們要爬取的是歌曲,既需要尋找歌曲的名字與其id信息,觀察可以發現其中一首歌單中的歌名-moon在類名為「f-hide」的ul標簽的li標簽的a標簽中) ~
E. Python爬蟲爬取QQ音樂的巔峰音樂榜,為什麼有的網頁成了這樣,我多試了幾次網頁爬取就載入不出來
圖片太模糊
注意大廠商做的網站往往有反爬機制 不能太頻繁去爬
F. 如何用爬蟲獲取網易雲音樂歌單中的歌曲
你好!看樣子你也是個萌萌噠的網易雲音樂愛好者啊!很高興為你解答,你可以按住鍵盤上的Ctrl不放,然後滑鼠左鍵點擊凡是你想要添加的歌曲,這樣就可以批量選定了,接著滑鼠右擊就會出現收藏到歌單了!全部選定請按Ctrl+A再滑鼠右擊!連續選定,請按住Shift,滑鼠分別左鍵單擊第一首和最後一首,然後滑鼠右鍵單擊就可以收藏啦!!
G. 如何用爬蟲爬取網頁上的數據
用爬蟲框架Scrapy, 三步
定義item類
開發spider類
開發pipeline
如果你想要更透的信息,你可以參考《瘋狂python講義》
H. python爬蟲下載音樂
用request不就行了嗎?
import requests
from playsound import playsound
class Music:
def save(self,url):
res=requests.get(url)
with open('music.mp3','wb') as f:
f.write(res.content)
if __name__ == '__main__':
url="https://music.163.com/song/media/outer/url?id=1456443773.mp3"
music=Music().save(url)
playsound("music.mp3")
I. 爬蟲爬取酷狗音樂為什麼爬取的音樂只有一段
摘要 想要爬取這些歌曲鏈接,然而這個是一個假的網站,雖然單機右鍵進行檢查能看到這些歌曲的鏈接,可進行爬取時,卻爬取不到這些信息。
J. 如何通過爬蟲實現對網易雲音樂用戶信息的抓取
可以從每首歌的評論作為切入點,採集用戶信息,具體實現過程說不清楚,如果你會編程,肯定明白怎麼寫爬蟲,如果不會,推薦你用軟體抓取數據。市面上很多採集軟體都比較好用(收費),價格一般也不算高,相比你要的數據的價值,軟體幾乎可以忽略不計。推薦幾個吧,Forespider(價格低速度快),熊貓採集器(好幾年的軟體了),火車頭