可以使用Python的requests库来发送HTTP请求获取网页内容。然后通过正则表达式或者BeautifulSoup等工具从HTML中提取所需数据。最后将提取到的数据存入MySQL数据库。

下面是一个示例代码:

python
import requests
import re
from bs4 import BeautifulSoup
import pymysql.cursors

# 定义要访问的URL
url = "http://example.com"

# 发送GET请求获取网页内容
response = requests.get(url)
html_content = response.text

# 使用正则表达式提取所需信息(这里只作为示例)
pattern = r'<div class="title">(.*?)</div>'
titles = re.findall(pattern, html_content)

# 连接MySQL数据库
connection = pymysql.connect(host='localhost', user='root', password='password', db='database')
cursor = connection.cursor()

try:
    # 遍历提取到的标题列表,并将其插入数据库
    for title in titles:
        sql = "INSERT INTO table (column1) VALUES ('%s');" % title
        cursor.execute(sql)
    
    # 提交事务
    connection.commit()
except Exception as e:
    print("Error:", str(e))
finally:
    # 关闭数据库连接
    cursor.close()
    connection.close()

以上就是python requests 爬取并提取内容写入mysql的详细内容,更多信息请关注OD云其它相关文章!



本文URL:http://www.odweb.cn/news_show.html?id=211