MySQL安装步骤:https://www.cnblogs.com/liuzengzhi/p/11704069.html
MySQL5.5 安装一直停在最后一步解决方法
首先,下载好MySQL,用pip安装好pymysql库,连接python和mysql。
pip install pymysql
import pymysql
import requests
from bs4 import BeautifulSoup
db = pymysql.connect(host="localhost",
port=3306,
user="root",
passwd="root",
database="scraping",
charset="utf8")
cursor = db.cursor()
link = "http://www.santostang.com/"
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36'}
r = requests.get(link, headers=headers)
soup = BeautifulSoup(r.text, "lxml")
title_list = soup.find_all("h1", class_="post-title")
for eachone in title_list:
url = eachone.a['href']
title = eachone.a.text.strip()
cursor.execute("INSERT INTO urls (url, content) VALUES (%s,%s)", (url, title))
db.commit()
db.close()