# -*- coding: utf-8 -*-
# !/usr/bin/env python
import requests
import re
from docx import Document
from docx.shared import Pt
from docx.oxml.ns import qn
t = Document()
p = t.add_paragraph(u'百度经验目录:\n')
u0 = 'http://jingyan.baidu.com/user/npublic?uid=d1b612bceb0dc22ba8ffe137&pn='
for x in range(0,999,7):
url = u0+str(x)#拼凑完整的网页链接
reponse = requests.get(url)#打开链接,抓取网页脚本
reponse.encoding='utf-8'#把抓取到的内容,转化为utf-8编码
html = reponse.text#再转化为文本
#从文本里面匹配文章标题
tts = re.findall(r'title="(.*?)" target="_blank">', html)
#从文本里面匹配每一篇文章的链接
u = re.findall(r'<a href="(/article/\w+\.html)" title="', html)
for i in range(len(u)):
run = p.add_run(tts[i]+'\n')
run = p.add_run('http://jingyan.baidu.com'+u[i]+'\n')
font = run.font
font.name = u'微软雅黑'
font.size = Pt(11)
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'微软雅黑')
t.save(u'C:/……/百度经验目录python.docx')
字体格式不太对。 用python给出百度经验的目录的方法
为防止上面的缩进出现混乱,请参考下图: