def get_mnnu_text(url): response = requests.get(url) soup = BeautifulSoup(response.content.decode('utf-8'), 'html.parser') content = soup.find_all('p') text = '\n'.join([p.get_text() for p in content]) 返回文本 URL = 'http://www.mnnu.edu.cn/' 文本 = get_mnnu_text(URL) 打印(文本) def save_to_navicat(text): connection = pymysql.connect( host='localhost', user='root', password='root', database='pa', charset='utf8' ) try: 使用connection.cursor()作为光标: SQL = “INSERT INTO () VALUES (%S)” encoded_text = TEXT.ENCODE('UTF8') cursor.execute(sql, (encoded_text,)) connection.commit() 最后: connection.close() save_to_navicat(text),我要修改url为 https://movie.douban.com/subject/30391186/comments?status=P
时间: 2024-04-02 07:37:52 浏览: 51
好的,你可以按照以下方式修改代码:
```python
import requests
from bs4 import BeautifulSoup
import pymysql
def get_movie_comments(url):
response = requests.get(url)
soup = BeautifulSoup(response.content.decode('utf-8'), 'html.parser')
content = soup.find_all('span', class_='short')
comments = '\n'.join([c.get_text() for c in content])
return comments
url = 'https://movie.douban.com/subject/30391186/comments?status=P'
comments = get_movie_comments(url)
def save_to_navicat(text):
connection = pymysql.connect(
host='localhost',
user='root',
password='root',
database='pa',
charset='utf8'
)
try:
with connection.cursor() as cursor:
sql = "INSERT INTO `my_table` (`content`) VALUES (%s)"
encoded_text = text.encode('utf8')
cursor.execute(sql, (encoded_text,))
connection.commit()
finally:
connection.close()
save_to_navicat(comments)
```
这样就可以获取豆瓣电影《误杀》的所有评论并保存到数据库中了。
阅读全文