事情是这样的,前段时间很喜欢看小说,上班地铁看下班地铁看。
但是免费的小说网站通过系统自带浏览器看又有很多乱七八糟的广告,什么重金求子,澳门d场等等,在地铁上人多眼杂的我又没贴防窥膜,旁边站个萌妹看到我手机上这种东西人家印象能好吗?不是影响我高大的形象????
刚好这个礼拜公司比较空闲,就想着学个爬虫去把小说爬到本地看
这边的话因为我是刚学习python如果有什么不专业的地方请指出
第一步
首先我在百度上随便找了一个免费小说的网站,第一步,我们都需要搜索小说嘛,所以先找搜索的入口
然后通过开发者工具F12可以找出查询的入口是这个地址
下面这段代码需要
pip install requests
pip install bs4
pip install fake_useragent
searchkey = input("请输入要查询的小说")
page_current = input("请输入开始章节")
page_end = input("请输入结束章节")
url = "https://www.xbiquge.la/modules/article/waps.php"
store_url = ""
user_agent = UserAgent().random
headers = {
"User-Agent": user_agent
}
data = {
"searchkey": searchkey
}
# 搜索小说
search_html = requests.post(url, headers=headers, data=data)
search_html.encoding = "utf-8"
soup = BeautifulSoup(search_html.text, 'lxml')
search_a = soup.find_all("a")
for i in search_a:
a_name = i.text
if a_name == searchkey:
store_url = i.attrs['href']
break
因为需要的路径在对应页面的的这个结构之下,我是通过Beautiful4来解析页面的
第二步
到了具体小说列表的页面,我所需要的是这个页面当中这些小说的具体链接的地址
而且根据具体章节页面后可以看出来地址是拼接在域名后面就可以访问
# 截取基础路径
a = store_url.index("/", 8, len(store_url))
base_url = store_url[0:a]
# 查询之后的页面
store_html = requests.get(store_url, headers)
store_html.encoding = "utf-8"
soup_store = BeautifulSoup(store_html.text, 'lxml')
div_store = soup_store.find_all("div")
first_store_url = ''
total_url_list = list()
for i in div_store:
if 'id' in i.attrs:
div_id = i.attrs['id']
if 'list' == div_id:
a_list = i.dl.find_all('dd')
for y in a_list:
total_url_list.append(base_url + y.a.attrs['href'])
break
pop_list = list()
for i in range(int(page_current), int(page_end) + 1):
if int(page_current) == 0:
pop_list.append(total_url_list.pop(int(page_current)))
else:
pop_list.append(total_url_list.pop(int(page_current) - 1))
第三步
终于到了最后一步了好激动啊,前面两布我大概花了3天时间,主要还是自己太笨了
这一步就可以爬取具体小说内容了!!!!!
一开始通过requests 爬取页面发现怎么都爬不出具体的内容
就是这一块东西通过requests是爬取不出来了,不过我还有百度,当天晚上回家地铁上没有看小说,百度搜了一下原因,可能是网站做了防爬机制,可以换selenium + chromedrive直接打开浏览器爬取
下面需要
pip install selenium
还有本地需要下载chromedrive到chrome目录并且配置到环境变量当中,chrome需要和chromedrive的版本一致,具体版本找不到大版本一致就行
chrome浏览器地址输入chrome://version/就可以查询到当前chrome的版本
到http://npm.taobao.org/mirrors/chromedriver/找chromedrive
# chromedriver的绝对路径
driver_path = r'C:\Program Files\Google\Chrome\Application\chromedriver.exe'
str_content = ''
total_count = 0
flag_count = 0
for i in pop_list:
# 初始化一个driver,并且指定chromedriver的路径
driver = webdriver.Chrome(executable_path=driver_path)
driver.get(i)
# 如果加载太快需要有个间隔,或者有些ajax请求不会一开始就加载出来也需要等待一下
# time.sleep(1)
i_store_soup = BeautifulSoup(driver.page_source, 'lxml')
i_store_div = i_store_soup.find_all('div')
for y in i_store_div:
if 'id' in y.attrs:
div_id = y.attrs['id']
if div_id == 'content':
flag_count = flag_count + 1
total_count = total_count + 1
page_name = int(page_current) + total_count
str_content = str_content + "------------------------------第" + str(
page_name) + "章------------------------------" + "\n" + y.text + "\n\n\n"
if flag_count == 100 or total_count == len(pop_list):
with open(searchkey + str(total_count) + ".txt", "w", encoding="utf-8") as f:
f.write(str_content)
f.close()
flag_count = 0
str_content = ''
break
driver.quit()
到此我的爬取小说任务结束了,愉快的去看小说了。
后期应该还会慢慢学习python其他的相关知识,这里的爬取应该来说还是挺简单的