爬取csdn博主文章
#!/usr/bin/env pythonimport reimport requestsimport osimport sysfrom lxml import etreeheaders = {"User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.1
·
#!/usr/bin/env python
import re
import requests
import os
import sys
from lxml import etree
headers = {
"User-Agent":
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36"
}
# 传递的非单个博客地址,而是博客列表
url = "https://blog.csdn.net/maqingqing1992"
html = requests.get(url, headers=headers, verify=False).text
nodes = etree.HTML(html)
tag_a = nodes.xpath('//p//a')
#文章摘要
digest_dic = []
url_dic1 = []
url_dic = []
for i in tag_a:
res = etree.tostring(i, method='text', encoding='utf-8').decode('utf-8')
digest_dic.append(res)
url_dic1.append(str(i.attrib))
for i in url_dic1:
url_dic.append(re.findall(u'https://.*\d', i))
fo = open("a.txt", "w")
for i in url_dic:
fo.write(i[0] + "\n")
fo.close()
只是爬取了文章的url,没有进一步利用。
更多推荐
已为社区贡献4条内容
所有评论(0)