Ai
1 Star 0 Fork 0

子安/Python-1

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
douban_book.py 1.37 KB
一键复制 编辑 原始数据 按行查看 历史
injetlee 提交于 2016-06-23 23:27 +08:00 . 0
from bs4 import BeautifulSoup
import requests
from openpyxl import Workbook
excel_name = "书籍.xlsx"
wb = Workbook()
ws1 = wb.active
ws1.title='书籍'
def get_html(url):
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0'}
html = requests.get(url, headers=header).content
return html
def get_con(html):
soup = BeautifulSoup(html,'html.parser')
book_list = soup.find('div', attrs={'class': 'article'})
page = soup.find('div', attrs={'class': 'paginator'})
next_page = page.find('span', attrs={'class': 'next'}).find('a')
name = []
for i in book_list.find_all('table'):
book_name = i.find('div', attrs={'class': 'pl2'})
m = list(book_name.find('a').stripped_strings)
if len(m)>1:
x = m[0]+m[1]
else:
x = m[0]
#print(x)
name.append(x)
if next_page:
return name, next_page.get('href')
else:
return name, None
def main():
url = 'https://book.douban.com/top250'
name_list=[]
while url:
html = get_html(url)
name, url = get_con(html)
name_list = name_list + name
for i in name_list:
location = 'A%s'%(name_list.index(i)+1)
print(i)
print(location)
ws1[location]=i
wb.save(filename=excel_name)
if __name__ == '__main__':
main()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/andrewgithub/Python-1.git
git@gitee.com:andrewgithub/Python-1.git
andrewgithub
Python-1
Python-1
master

搜索帮助