在元素选择器中有两个参数,第一个参数是匹配标签的表达式,第二个参数就是属性的名字。
webmagic模拟登录方案:方案一:模拟浏览器登录,用代码模拟表单填写,然后获取登陆后的信息,用apache的“HttpClients”进行信息保存。不需要考虑cookie失效问题。方案二:直接拿去cookie信息,进行设置。可能会用时间限制,超过一定时间就不能再使用了,需要重新设置。
方案一实现:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<code>import java.io.BufferedReader
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.util.ArrayList
import java.util.List
import org.apache.http.HttpResponse
import org.apache.http.NameValuePair
import org.apache.http.client.ClientProtocolException
import org.apache.http.client.methods.HttpPost
import org.apache.http.client.methods.HttpUriRequest
import org.apache.http.message.BasicNameValuePair
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element
import com.wgyscsf.utils.HttpUtils
public class SimulateLoginPolicy1 {
static boolean result = false
public static void main(String[] args) {
loginCsdnPager()
try {
loginedPager()
} catch (ClientProtocolException e) {
e.printStackTrace()
} catch (IOException e) {
e.printStackTrace()
}
}
private static void loginedPager() throws IOException,
ClientProtocolException {
HttpUriRequest httpUriRequest = new HttpPost(
"http://blog.csdn.net/wgyscsf")
httpUriRequest
.setHeader("Accept",
"text/html,application/xhtml+xml,application/xmlq=0.9,image/webp,*/*q=0.8")
httpUriRequest.setHeader("Accept-Encoding", "gzip,deflate,sdch")
httpUriRequest.setHeader("Accept-Language", "zh-CN,zhq=0.8")
httpUriRequest.setHeader("Connection", "keep-alive")
// 模拟浏览器,否则CSDN服务器限制访问
httpUriRequest
.setHeader(
"User-Agent",
"Mozilla/5.0 (Windows NT 6.1WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36")
HttpResponse response = HttpUtils.httpClient.execute(httpUriRequest)
InputStream content = response.getEntity().getContent()
BufferedReader br = new BufferedReader(new InputStreamReader(content))
String line = ""
String result = ""
while ((line = br.readLine()) != null) {
result += line
}
br.close()
System.out.println(result)
}
/**
* 登录页面
*/
private static void loginCsdnPager() {
String html = HttpUtils
.sendGet("https://passport.csdn.net/account/login?ref=toolbar")// 这个是登录的页面
Document doc = Jsoup.parse(html)
// 获取表单所在的节点
Element form = doc.select(".user-pass").get(0)
String lt = form.select("input[name=lt]").get(0).val()
String execution = form.select("input[name=execution]").get(0).val()
String _eventId = form.select("input[name=_eventId]").get(0).val()
List<namevaluepair>nvps = new ArrayList<namevaluepair>()
nvps.add(new BasicNameValuePair("username", "wgyscsf@163.com"))
nvps.add(new BasicNameValuePair("password", "wanggaoyuan"))
nvps.add(new BasicNameValuePair("lt", lt))
nvps.add(new BasicNameValuePair("execution", execution))
nvps.add(new BasicNameValuePair("_eventId", _eventId))
String ret = HttpUtils.sendPost(
"https://passport.csdn.net/account/login?ref=toolbar", nvps)
if (ret.indexOf("redirect_back") >-1) {
System.out.println("登陆成功")
result = true
} else if (ret.indexOf("登录太频繁") >-1) {
System.out.println("登录太频繁,请稍后再试。。。。。")
return
} else {
System.out.println("登陆失败。")
return
}
}
}</namevaluepair></namevaluepair></code>
方案二:使用浏览器的开发者工具将cookie手动加进去
<?$db_host = "localhost"//链接的数据库地址,也就是主机名字
$db_user = "db"//数据库名字
$db_pass = "数据库密码"
$db_name = "msg"//表名
$connec = mysql_connect($db_host,$db_user,$db_pass) or die("不能连接数据库服务器: ".mysql_error())
mysql_select_db($db_name,$connec) or die ("不能选择数据库: ".mysql_error())
$user=$_POST['user']//$_post不用大写的就没用得
$sms=$_POST['sms']
$ID=$_POST['id']
$db_query='INSERT INTO msg(表名) VALUES $user,$sms,$ID'//插入
mysql db query($db_query)//运行sql语句
?>
上面的程序改改就可以用了,或许有问题,我在网吧,没调试的!
我也是学PHP的,现在还很菜,有时间的话咱交流交流!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)