爬取博客园文章的两个案例,写入sql server数据库
1,爬取指定用户发表的文章前50页,不足50页则全部爬取,提取标题,摘要,文章链接,提交详情; 代码如下: import pymssql import requests from lxml import etree import re import time headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36' } # 博客园我的文章(标题,摘要,文章链接,提交详情) # 连接sql server数据库 conn = pymssql.connect(host='127.0.0.1', user='sa', password='root', database='a', charset='utf8') cursor = conn.cursor() # 创建数据表id设为主键自增 cursor.execute(""" if object_id('years','U') is not null drop table years create table years( id int not null primary key IDENTITY(1,1),