爬虫爬小说
本帖最后由 君子好逑 于 2020-8-22 22:18 编辑import requests
import bs4
import re
def open_html(url):
headers = {
'Accept-Language': 'zh-CN',
'Cache-Control': 'no-cache',
'Connection': 'Keep-Alive',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18363'
}
res = requests.get(url=url,headers=headers)
html = res.text
return html
def get_mulu(): #拿到小说目录的地址
url1 = 'https://www.biduo.cc/search.php?q='
name = input("请输入小说名:")
global f
f = open(name + '.txt','w')
url = url1 + name
headers = {
'Accept-Language': 'zh-CN',
'Cache-Control': 'no-cache',
'Connection': 'Keep-Alive',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18363'
}
res = requests.get(url,headers=headers)
s = bs4.BeautifulSoup(res.text,features="lxml")
xiaoshuo = s.find("h3",class_="result-item-title result-game-item-title") #找到 第一个 标签(即为搜索榜一) 找全部可用s.find_all
url_ = xiaoshuo.find("a").get ('href')# 在获得的标签中 继续找到 a 标签,并get 到 href 属性
return ("https://www.biduo.cc" + url_) #加前缀
def get_html(url):
headers = {
'Accept-Language': 'zh-CN',
'Cache-Control': 'no-cache',
'Connection': 'Keep-Alive',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18363'
}
response = requests.get(url=url,headers=headers)
html = response.text
return html
def get_first(url):
html = open_html(url)
regular1 = re.compile('<dd><a href="(.*?)">.*?</a></dd>')
list1 = regular1.findall(html)
url_1 = 'https://www.biduo.cc'
for each in list1:
url = url_1 + each
html = open_html(url)
if ('第一章') in html:
break
return url
def get_text(html):
regular = re.compile("(.*?) ")
m = regular.findall(html)
if m:
m.pop(0)
for each in m:
print(each.split('<br>'))
f.write(each.split('<br>'))
f.write('\n')
def get_next(html):
url_1 = 'https://www.biduo.cc'
regular = re.compile(r'.*?">上一章</a> ← <a href=".*?">章节列表</a> .*?; <a href="(.*?)"')
m = regular.findall(html)
url_2 = m.pop()
next_address = url_1 + url_2
return next_address
def get_title(html):
title = re.findall('<h1>(.*?)</h1>',html)
title = title
return title
def main():
catalog_address = get_mulu()
number = int(input('请输入您想获取的章节总数:'))
url = get_first(catalog_address)
for each in range(number):
html = get_html(url)
title = get_title(html)
next_address = get_next(html)
f.write(title)
f.write('\n')
f.write('\n')
get_text(html)
url = next_address
f.write('\n')
f.write('\n')
f.close()
if __name__ == "__main__":
main()
在大佬们的帮助下初步完成了这个程序。爬了无敌真寂寞的前三章和元尊的前六章(因为元尊的前三章都是更新预告),倒是还行。谢谢大佬们的指点。就是爬完之后发现爬的挺慢的,希望大佬们能给我一些改进的意见{:10_256:}
如果感觉爬的慢就去试试多线程吧,下面代码没有改进只是把你代码中一些地方改动了下
比如文件写入直接写入两个 \n 就好,你却写了两行 f.write('\n') 哈哈
还有 headers 都是同一个,那么放全局作用域就好,不需要一个个方法来设置局部变量了
其他地方都没有改动,所以说没有把你的代码进行改进了
import requests
import bs4
import re
def open_html(url):
res = requests.get(url=url, headers=headers)
html = res.text
return html
def get_mulu():# 拿到小说目录的地址
url1 = 'https://www.biduo.cc/search.php?q='
name = input("请输入小说名:")
global f
f = open(name + '.txt', 'w')
url = url1 + name
res = requests.get(url, headers=headers)
s = bs4.BeautifulSoup(res.text, features="lxml")
xiaoshuo = s.find("h3", class_="result-item-title result-game-item-title")# 找到 第一个 标签(即为搜索榜一) 找全部可用s.find_all
url_ = xiaoshuo.find("a").get('href')# 在获得的标签中 继续找到 a 标签,并get 到 href 属性
return ("https://www.biduo.cc" + url_)# 加前缀
def get_html(url):
response = requests.get(url=url, headers=headers)
html = response.text
return html
def get_first(url):
html = open_html(url)
regular1 = re.compile('<dd><a href="(.*?)">.*?</a></dd>')
list1 = regular1.findall(html)
url_1 = 'https://www.biduo.cc'
for each in list1:
url = url_1 + each
html = open_html(url)
if ('第一章') in html:
break
return url
def get_text(html):
regular = re.compile("(.*?) ")
m = regular.findall(html)
if m:
m.pop(0)
for each in m:
print(each.split('<br>'))
f.write(each.split('<br>'))
f.write('\n')
def get_next(html):
url_1 = 'https://www.biduo.cc'
regular = re.compile(r'.*?">上一章</a> ← <a href=".*?">章节列表</a> .*?; <a href="(.*?)"')
m = regular.findall(html)
url_2 = m.pop()
next_address = url_1 + url_2
return next_address
def get_title(html):
title = re.findall('<h1>(.*?)</h1>', html)
title = title
return title
def main():
catalog_address = get_mulu()
number = int(input('请输入您想获取的章节总数:'))
url = get_first(catalog_address)
for each in range(number):
html = get_html(url)
title = get_title(html)
next_address = get_next(html)
f.write(title)
f.write('\n\n')
get_text(html)
url = next_address
f.write('\n\n')
f.close()
if __name__ == "__main__":
headers = {
'Accept-Language': 'zh-CN',
'Cache-Control': 'no-cache',
'Connection': 'Keep-Alive',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18363'
}
main() Twilight6 发表于 2020-8-22 22:31
如果感觉爬的慢就去试试多线程吧,下面代码没有改进只是把你代码中一些地方改动了下
比如文件写入直 ...
ok,谢谢大佬指点{:10_257:} 爬的慢就用Scrapy,一般这种大型的项目用Scrapy比较好,我爬1000多章在1分钟之内搞定 lhgzbxhz 发表于 2020-8-23 15:51
爬的慢就用Scrapy,一般这种大型的项目用Scrapy比较好,我爬1000多章在1分钟之内搞定
ok,主要是视频还没看到那么多{:10_256:}
页:
[1]