import pyodbc
import requests
from lxml import etree
# DRIVER={SQL Server};DATABASE=数据库名;SERVER=服务器所在的计算机名,端口号;UID=你的用户名;PWD=你的密码
odbc = 'DRIVER={SQL Server};SERVER=127.0.0.1,1433;DATABASE=pymssql;UID=***;PWD=*****'
# 1.链接数据库
db = pyodbc.connect(odbc, autocommit=True,) # 使用 cursor()方法创建一个游标对象 cursor
# 2.游标
cursor = db.cursor() # 使用 execute()方法执行 SQL 查询
url = 'https://www.3dst.cn/t/lizhigushi/'
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Edg/106.0.1370.47'
}
res = requests.get(url, headers=headers)
data = res.content.decode()
# print(data)
tree = etree.HTML(data)
a_list = tree.xpath('/html/body/main/div/div[2]/ul/li/a/@href')
# print(a_list)
b = 1
for a in a_list:
res = requests.get(a, headers=headers)
python爬取数据存储在sql sever数据库中
最新推荐文章于 2025-03-17 18:28:24 发布