Python爬⾍之爬取某⽂库⽂章
⽂章⽬录
说在前⾯的话:
本篇笔记准确地说应该是⼀篇改进版⽂章。
制作滚动图片其来源于:
本⽂所爬取的⽂章url:
需要注意的是,源码中加⼊很多 time.sleep() 语句,其⽬的是防⽌被封IP,即更好的将机器伪装成⼈为操作。所以,在运⾏时速度缓慢,请在没有出现报错时,耐⼼等待。
如若侵权,联系即删
⼀、源码展⽰:
fwrite写结构体
这是作者 ⽂章中的源代码,当我们在运⾏时,会出现如下图所⽰的报错:
注:在运⾏时,我们除了所使⽤代码中的库时,还需⽤⽤到⼀个软件,即 。这是在使⽤ selenium 必备的。
其具体安装使⽤情况,详见:
"""author
鹏鹏写代码
"""
import pandas
import selenium
from selenium.webdrivermon.keys import Keys
from selenium import webdriver
l.ns import qn  # 中⽂格式
from docx import Document  ##需要安装第三⽅库python-docx
from docx.shared import Pt  # ⽤于设置字体样式
import time
driver = webdriver.Chrome("C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\Application\\") ("wenku.baidu/view/9f9765542d60ddccda38376baf1ffc4fff47e26c")
time.sleep(5)
#为了避免百度页⾯变为旧版页⾯,需要刷新
time.sleep(2)busybox安装工具
driver.find_element_by_xpath("//div[@class='fold-page-text']").click()#点击“阅读所有页⾯“
#得到当前总页⾯
all_page = driver.find_element_by_xpath("//div[@class='goto-page']").place("/ ","")
result_text =""
i =1
while(i <=int(all_page)):
driver.find_element_by_xpath("//input[@class='cur-page']").clear()#清除输⼊值
driver.find_element_by_xpath("//input[@class='cur-page']").send_keys(i)#设置跳转页⾯编号
driver.find_element_by_xpath("//input[@class='cur-page']").send_keys(Keys.ENTER,'\ue007')# 回车键
time.sleep(2)
result_text = result_text + driver.find_element_by_xpath("//div[@id='pageNo-{0}']".format(i)).text  #得到页⾯的⽂本
time.sleep(2)
i +=1
maven编译命令result_text = place("\n","")
zf =0
while zf <=9:
s =str(zf)+"、"
# print (s)
result_text = place(s,"\n"+ s)
zf +=1
for zf_s in('⼀','⼆','三','四','五','六','七','A','B','C','D','E'):
s = zf_s +"、"
t = zf_s +"."
result_text = place(s,"\n"+ s).replace(t,"\n"+ t)
result_text = place("\n\n","\n")
docx_path ="课程设计.docx"
doc = Document()
doc.styles["Normal"].font.name =u"宋体"# 设置字体样式
doc.styles["Normal"].font.size = Pt(14)# 设置字体⼤⼩
doc.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'),u'宋体')# 设置⽂档的基础样式
doc.add_paragraph(result_text)# 增加⼀个paragraph,写⼊内容
doc.save(docx_path)# 保存⽂档
其原因是可能⽂库进⾏了更新,当我们在点击 继续阅读 时,会弹出需要登录。
所以本⽂是在上⾯代码的基础上,解决其登录问题。同时,本⼈似乎也发现了某库在登录时的⼀个⼩⼩bug。且听我来慢慢细说。⼆、完整代码展⽰及运⾏效果图
1.完整代码:
# -*- codeing=utf-8 -*-
# @Time:2021/7/30 12:06
# @Atuhor:@lwtyh
# @File:demo.py
# @Software:PyCharm
import pandas
import selenium
from selenium.webdrivermon.keys import Keys
from selenium import webdriver
l.ns import qn  # 中⽂格式
from docx import Document  # 需要安装第三⽅库python-docx
from docx.shared import Pt  # ⽤于设置字体样式
import time
driver = webdriver.Chrome(".\")# 将保存在当前⽬录下
<("wenku.baidu/view/9f9765542d60ddccda38376baf1ffc4fff47e26c")
<("wenku.baidu/view/9f9765542d60ddccda38376baf1ffc4fff47e26c")
time.sleep(3)
driver.maximize_window()# ⾃动将⽹页放⼤⾄最⼤化
# 为了避免百度页⾯变为旧版页⾯,需要刷新
time.sleep(2)
# 点击登录
account_login_button = driver.find_element_by_xpath('//div[@class="right-box"]/div[4]')
account_login_button.click()
# 账号密码登录
time.sleep(3)
account_login_button = driver.find_element_by_xpath('//div[@class="tang-pass-footerBar"]/p[2]')
account_login_button.click()
time.sleep(4)
# 输⼊账号
input_account = driver.find_element_by_id('TANGRAM__PSP_11__userName')
input_account.send_keys('请输⼊账号')
time.sleep(4)
# 输⼊密码
input_password = driver.find_element_by_id('TANGRAM__PSP_11__password')
input_password.send_keys('请输⼊密码')
time.sleep(2)
# 点击登录按钮
login_button = driver.find_element_by_id('TANGRAM__PSP_11__submit')
login_button.click()
time.sleep(2)
account_login_button = driver.find_element_by_xpath('//div[@class="vcode-body vcode-body-spin"]/div[2]')# 去掉验证account_login_button.click()
# 点击登录按钮(重新)
time.sleep(3)
login_button = driver.find_element_by_id('TANGRAM__PSP_11__submit')
login_button.click()
time.sleep(5)
account_login_button = driver.find_element_by_xpath('//*[@id="app"]/div[3]/div[3]/div[4]/div/div[2]/i')# 去掉⼴告account_login_button.click()
time.sleep(2)
time.sleep(3)
driver.find_element_by_xpath("//div[@class='fold-page-text']").click()# 点击“阅读所有页⾯“
time.sleep(2)
#得到当前总页⾯
all_page = driver.find_element_by_xpath("//div[@class='goto-page']").place("/ ","")
result_text =""
i =1
while(i <=int(all_page)):
driver.find_element_by_xpath("//input[@class='cur-page']").clear()#清除输⼊值
driver.find_element_by_xpath("//input[@class='cur-page']").send_keys(i)#设置跳转页⾯编号
driver.find_element_by_xpath("//input[@class='cur-page']").send_keys(Keys.ENTER,'\ue007')# 回车键
time.sleep(2)
result_text = result_text + driver.find_element_by_xpath("//div[@id='pageNo-{0}']".format(i)).text  #得到页⾯的⽂本
time.sleep(2)
i +=1
i +=1
result_text = place("\n","")
zf =0
while zf <=9:
s =str(zf)+"、"
# print (s)
result_text = place(s,"\n"+ s)
zf +=1
for zf_s in('⼀','⼆','三','四','五','六','七','A','B','C','D','E'):
s = zf_s +"、"
t = zf_s +"."
爬虫软件 appresult_text = place(s,"\n"+ s).replace(t,"\n"+ t)
result_text = place("\n\n","\n")odbc api 错误代码
docx_path ="课程设计.docx"
doc = Document()
doc.styles["Normal"].font.name =u"宋体"# 设置字体样式
doc.styles["Normal"].font.size = Pt(14)# 设置字体⼤⼩
doc.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'),u'宋体')# 设置⽂档的基础样式
doc.add_paragraph(result_text)# 增加⼀个paragraph,写⼊内容
doc.save(docx_path)# 保存⽂档
2.效果图:
三、解析源码:
1. 问题解决:
问题: 前⽂说到,当我们在点击 继续阅读 时,会弹出需要登录账号验证才⾏,否则将不能继续阅读后⾯的页码,也就会出现上图所⽰的报错,导致程序⽆法进⾏正常运⾏。
解决⽅法: 俗话说,“办法总⽐困难多”。解决这个问题,⼀个很简单的⽅法想多不⽤想,便是进⾏登录即可。所以便有了后续的代码。
2.⾃动登录:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。