怎么将这段python代码采集到的数据插入数据库的表里:for detail_url in t_url2: res = requests.get(url=detail_url, headers=headers) res.encoding = 'utf-8' tree = html.fromstring(res.text) name2 = tree.xpath('/html/body/main/div[1]/div[1]/span/text()') article2 = tree.xpath('/html/body/main/div[1]/div[3]//text()') picture_str2 = html.etree.tostring(article2[0], encoding='utf-8', method='html').decode('utf-8') 如何编写python代码
时间: 2024-03-02 14:50:01 浏览: 236
pandas_orm-0.0.1.tar.gz
你可以使用Python的MySQL Connector来将采集到的数据插入到MySQL数据库表中。以下是一个基本的示例代码:
```python
import mysql.connector
import requests
from lxml import html
# 连接到MySQL数据库
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
# 遍历URL列表并采集数据
for detail_url in t_url2:
res = requests.get(url=detail_url, headers=headers)
res.encoding = 'utf-8'
tree = html.fromstring(res.text)
name2 = tree.xpath('/html/body/main/div[1]/div[1]/span/text()')[0]
article2 = tree.xpath('/html/body/main/div[1]/div[3]//text()')
picture_str2 = html.etree.tostring(article2[0], encoding='utf-8', method='html').decode('utf-8')
# 插入数据到数据库表中
sql = "INSERT INTO mytable (name, article, picture) VALUES (%s, %s, %s)"
val = (name2, article2, picture_str2)
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "条记录已插入。")
```
在这个示例中,我们使用了requests和lxml库来采集网页数据,然后将采集到的数据插入到名为“mytable”的MySQL数据库表中。你需要根据自己的需求,修改代码中的数据库连接信息、插入的表名和字段名等,以适应不同的情况。
阅读全文