爬虫项目(2)——微博个人信息获取

selenium爬虫项目(2)——微博个人信息获取

Introduction

最近事情有点多,鸽到了现在(其实是国庆玩嗨了)。最近抽时间把第二部分功能基本完成了,虽然后续代码可能会重构一次,不过还是写一次blog记录一下。
xthGr9.png

step1 登录之后搜索用户的id,并选择第一条检索记录进入

这里以搜索用户id“等风”为例。

Code:

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
from selenium import webdriver
import time
import sys

non_bmp_map=dict.fromkeys(range(0x10000,sys.maxunicode+1),0xfffd)
if __name__ == '__main__':

chrome_driver='C:\\Users\\lenovo\\Anaconda3\\Lib\\site-packages\\chromedriver.exe'
driver = webdriver.Chrome(executable_path = chrome_driver)
driver.get('https://www.baidu.com')
#driver.get('https://weibo.com/u/7791772275/home?wvr=5')
try:
driver.find_element_by_xpath('//*[@id="kw"]').click()
driver.find_element_by_xpath('//*[@id="kw"]').send_keys('微博')
time.sleep(3)
driver.find_element_by_xpath('//*[@id="su"]').click()
time.sleep(2)

driver.find_element_by_xpath('//*[@id="1"]/div/div[1]/h3/a[1]').click()
time.sleep(10)

handles=driver.window_handles
for handle in handles:
if handle!=driver.current_window_handle:
driver.close()
driver.switch_to.window(handle)
driver.execute_script("window.scrollBy(0,3000)")
time.sleep(5)
driver.find_element_by_xpath('//*[@id="app"]/div[1]/div[1]/div[2]/div[1]/div/div/div[3]/div[1]/div/a[1]').click()
time.sleep(5)

driver.find_element_by_xpath('//*[@id="app"]/div[4]/div[1]/div/div[2]/div/div/div[5]/a[1]').click()

handles=driver.window_handles
for handle in handles:
if handle!=driver.current_window_handle:
driver.close()
driver.switch_to.window(handle)
time.sleep(10)


driver.find_element_by_id("loginname").click()
driver.find_element_by_id("loginname").send_keys("你的账号")
driver.find_element_by_xpath('//*[@id="pl_login_form"]/div/div[3]/div[2]/div/span').click()
driver.find_element_by_xpath('//*[@id="pl_login_form"]/div/div[3]/div[2]/div/input').send_keys('你的密码')
time.sleep(5)
driver.find_element_by_class_name('W_btn_a').click()
time.sleep(5)

driver.find_element_by_xpath('//*[@id="dmCheck"]').click()
time.sleep(1)
driver.find_element_by_id('send_dm_btn').click()
time.sleep(20)

driver.find_element_by_xpath('//*[@id="plc_top"]/div/div/div[2]/input').click() #点击搜索框
time.sleep(20)
driver.find_element_by_xpath('//*[@id="plc_top"]/div/div/div[2]/input').send_keys("等风") #输入“等风”
time.sleep(10)
driver.find_element_by_xpath('//*[@id="plc_top"]/div/div/div[2]/a').click() #点击搜索
time.sleep(2)
driver.find_element_by_xpath('/html/body/div[1]/div[2]/ul/li[2]/a').click() #点击“找人”按钮
driver.find_element_by_xpath('//*[@id="pl_user_feedList"]/div[1]/div[2]/div/a[1]').click() #点击第一条检索记录

time.sleep(5)
handles=driver.window_handles
for handle in handles: #关闭之前的界面
if handle!=driver.current_window_handle:
driver.close()
driver.switch_to.window(handle)
time.sleep(10)
finally:
time.sleep(30)
driver.quit()

Explanation

这一步也就是基本操作,找到按键的xpath,调用点击相应位置即可,结果如图:

xthBxe.png

step2 进入用户的个人信息界面并保存页面源代码

Code:

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
from selenium import webdriver
import time
import sys

non_bmp_map=dict.fromkeys(range(0x10000,sys.maxunicode+1),0xfffd)
if __name__ == '__main__':

chrome_driver='C:\\Users\\lenovo\\Anaconda3\\Lib\\site-packages\\chromedriver.exe'
driver = webdriver.Chrome(executable_path = chrome_driver)
driver.get('https://www.baidu.com')
#driver.get('https://weibo.com/u/7791772275/home?wvr=5')
try:
driver.find_element_by_xpath('//*[@id="kw"]').click()
driver.find_element_by_xpath('//*[@id="kw"]').send_keys('微博')
time.sleep(3)
driver.find_element_by_xpath('//*[@id="su"]').click()
time.sleep(2)

driver.find_element_by_xpath('//*[@id="1"]/div/div[1]/h3/a[1]').click()
time.sleep(10)

handles=driver.window_handles
for handle in handles:
if handle!=driver.current_window_handle:
driver.close()
driver.switch_to.window(handle)
driver.execute_script("window.scrollBy(0,3000)")
time.sleep(5)
driver.find_element_by_xpath('//*[@id="app"]/div[1]/div[1]/div[2]/div[1]/div/div/div[3]/div[1]/div/a[1]').click()
time.sleep(5)

driver.find_element_by_xpath('//*[@id="app"]/div[4]/div[1]/div/div[2]/div/div/div[5]/a[1]').click()

handles=driver.window_handles
for handle in handles:
if handle!=driver.current_window_handle:
driver.close()
driver.switch_to.window(handle)
time.sleep(10)


driver.find_element_by_id("loginname").click()
driver.find_element_by_id("loginname").send_keys("你的账号")
driver.find_element_by_xpath('//*[@id="pl_login_form"]/div/div[3]/div[2]/div/span').click()
driver.find_element_by_xpath('//*[@id="pl_login_form"]/div/div[3]/div[2]/div/input').send_keys('你的密码')
time.sleep(5)
driver.find_element_by_class_name('W_btn_a').click()
time.sleep(5)

driver.find_element_by_xpath('//*[@id="dmCheck"]').click()
time.sleep(1)
driver.find_element_by_id('send_dm_btn').click()
time.sleep(20)

driver.find_element_by_xpath('//*[@id="plc_top"]/div/div/div[2]/input').click()
time.sleep(20)
driver.find_element_by_xpath('//*[@id="plc_top"]/div/div/div[2]/input').send_keys("等风")
time.sleep(10)
driver.find_element_by_xpath('//*[@id="plc_top"]/div/div/div[2]/a').click()
time.sleep(2)
driver.find_element_by_xpath('/html/body/div[1]/div[2]/ul/li[2]/a').click()
driver.find_element_by_xpath('//*[@id="pl_user_feedList"]/div[1]/div[2]/div/a[1]').click()

time.sleep(5)
handles=driver.window_handles
for handle in handles:
if handle!=driver.current_window_handle:
driver.close()
driver.switch_to.window(handle)
time.sleep(10)

driver.find_element_by_xpath('//*[@id="Pl_Core_UserInfo__6"]/div[2]/div[1]/div/a/span').click() #点击“查看更多”获取更多信息
time.sleep(5)
handles=driver.window_handles
for handle in handles: #关闭之前的界面
if handle!=driver.current_window_handle:
driver.close()
driver.switch_to.window(handle)
time.sleep(10)
source=driver.page_source #获取页面源代码
with open("information.txt","w",encoding='utf-8') as f:
f.write(str(source).translate(non_bmp_map)) #这里的处理方式是为了能让中文正确编码写入文件之中

#driver.find_element_by_xpath('//*[@id="Pl_Core_T8CustomTriColumn__50"]/div/div/div/table/tbody/tr/td[1]/a/strong').click()



finally:
time.sleep(30)
driver.quit()

Explanation

这一步基本上也是不断用xpath点击,但是需要注意的是获取页面源代码之前要将页面句柄调整到正确的位置,否则获取的页面可能不是你想要的,我采取的是直接关掉之前的页面。还有一点就是如果直接把源代码转成字符串存到文件里会发现报错,解决方法如上。

step3 从存储的html文件中提取出所需要的信息

Code:

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
from lxml import etree

with open("information.txt","r",encoding='utf-8') as f:
data=f.read()

html=etree.HTML(data)

for i in range(1,10):
tmp1=html.xpath('//*[@id="Pl_Official_PersonalInfo__54"]/div[1]/div/div[2]/div/ul/li[%d]/span[1]'%i)
tmp=html.xpath('//*[@id="Pl_Official_PersonalInfo__54"]/div[1]/div/div[2]/div/ul/li[%d]/span[2]'%i)
for j in range(len(tmp)):
print(str(tmp1[j].text).strip()+' : ' +str(tmp[j].text).strip())


for i in range(1,10):
tmp1=html.xpath('//*[@id="Pl_Official_PersonalInfo__54"]/div[2]/div/div[2]/div/ul/li[%d]/span[1]'%i)
tmp=html.xpath('//*[@id="Pl_Official_PersonalInfo__54"]/div[2]/div/div[2]/div/ul/li[%d]/span[2]'%i)

for j in range(len(tmp)):
print(str(tmp1[j].text).strip()+' : ' +str(tmp[j].text).strip())



for i in range(1,10):
tmp1=html.xpath('//*[@id="Pl_Official_PersonalInfo__54"]/div[3]/div/div[2]/div/ul/li[%d]/span[1]'%i)
tmp=html.xpath('//*[@id="Pl_Official_PersonalInfo__54"]/div[3]/div/div[2]/div/ul/li[%d]/span[2]/a'%i)

for j in range(len(tmp)):
print(str(tmp1[j].text).strip()+' : ' +str(tmp[j].text).strip())

xpath='//span[@class="pt_detail"]/a[@target="_blank"]/text()'
a=[]
b=html.xpath(xpath)
res=[]
for i in range(len(b)):
res+=b[i].split()
print("标签信息",res)

Explanation

这一步需要了解lxml库的基本用法,同时也要去网上搜索学习一下xpath语法,然后基于xpath语法用lxml库筛选出我们所需要的信息。由于微博个人信息界面的html不太规整,因此上面的代码大多数情况下可以正确运行,但是在出现某些特殊结构的时候不能正确工作。这一步留到后面加以改进,尽量做到信息不重不漏。
例:
xthrKH.png


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!