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]) return text url = 'http://www.mnnu.edu.cn/' text = get_mnnu_text(url) print(text) 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(text),我要修改url为https://movie.douban.com/subject/30391186/comments?start={}&limit=20&sort=new_score&status=P
时间: 2024-04-02 13:37:55 浏览: 58
好的,你可以按照以下方式修改代码:
```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_template = 'https://movie.douban.com/subject/30391186/comments?start={}&limit=20&sort=new_score&status=P'
comments = ''
for i in range(0, 100, 20): # 获取前100条评论
url = url_template.format(i)
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)
```
这样就可以获取豆瓣电影《误杀》的前100条评论并保存到数据库中了。
阅读全文