给我做一个python 爬虫,…
- User
给我做一个python 爬虫, 网页地址为http://va.rizhao.gov.cn/col/col321812/index.html,要求先爬取双拥工作板块中的文章列表,
<span>双拥工作</span> <a href="/col/col82374/index.html" title="双拥工作更多">更多>></a> </div> <div class="case_box_list"> <div class="list" style="display: block;" ></div> </div>需要抓到的内容为2023-04-01并打开对应的链接爬取文章内容,存储到MYSQL数据库,数据格式为标题、发布日期、信息来源、文章内容,
- Assistant
以下是Python爬虫代码:
import requests from bs4 import BeautifulSoup import pymysql # 连接数据库 db = pymysql.connect(host='localhost', user='root', password='123456', db='test', charset='utf8') cursor = db.cursor() # 发送请求获得网页内容 url = 'http://va.rizhao.gov.cn/col/col321812/index.html' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0;Win64)AppleWebKit/537.36(KHTML,likeGecko) Chrome/58.0.3029.110Safari/537.3 Edge/16.16299' } response = requests.get(url, headers=headers) # 解析网页内容 soup = BeautifulSoup(response.text, 'html.parser') div = soup.find('div', attrs={'class': 'news_case_center', 'data-wza-text': '双拥工作'}) a_list = div.find('div', attrs={'class': 'list'}).find_all('a') for a in a_list: article_url = 'http://va.rizhao.gov.cn' + a['href'] article_date = a.find('span', attrs={'class': 'time'}).get_text() article_title = a.find('span', attrs={'class': 'con'}).get_text() # 发送请求获取文章内容 response_article = requests.get(article_url, headers=headers) soup_article = BeautifulSoup(response_article.text, 'html.parser') article_content = soup_article.find('div', attrs={'class': 'news_case_content'}).get_text().strip() article_source = soup_article.find('div', attrs={'class': 'pd10'}).get_text().strip().split('来源:')[1] # 存储到数据库 sql = "INSERT INTO article(title, date, source, content) VALUES(%s, %s, %s, %s)" try: cursor.execute(sql, (article_title, article_date, article_source, article_content)) db.commit() except Exception as e: db.rollback() print(e) # 关闭数据库连接 db.close() - User
- Assistant
以下是Python爬虫代码:
import requests from bs4 import BeautifulSoup import pymysql import datetime # 获取今天的日期 today = datetime.date.today() # 连接数据库 db = pymysql.connect(host='localhost', user='root', password='123456', db='test', charset='utf8') cursor = db.cursor() # 发送请求获得网页内容 url = 'http://va.rizhao.gov.cn/col/col321812/index.html' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0;Win64)AppleWebKit/537.36(KHTML,likeGecko) Chrome/58.0.3029.110Safari/537.3 Edge/16.16299' } response = requests.get(url, headers=headers) # 解析网页内容 soup = BeautifulSoup(response.text, 'html.parser') div = soup.find('div', attrs={'class': 'news_case_center', 'data-wza-text': '双拥工作'}) a_list = div.find('div', attrs={'class': 'list'}).find_all('a') for a in a_list: article_url = 'http://va.rizhao.gov.cn' + a['href'] article_date = datetime.datetime.strptime(a.find('span', attrs={'class': 'time'}).get_text(), "%Y-%m-%d").date() if article_date == today: article_title = a.find('span', attrs={'class': 'con'}).get_text() # 发送请求获取文章内容 response_article = requests.get(article_url, headers=headers) soup_article = BeautifulSoup(response_article.text, 'html.parser') article_content = soup_article.find('td', attrs={'class': 'bt_content'}).get_text().strip() article_source = soup_article.find('div', attrs={'class': 'pd10'}).get_text().strip().split('来源:')[1] # 存储到数据库 sql = "INSERT INTO article(title, date, source, content) VALUES(%s, %s, %s, %s)" try: cursor.execute(sql, (article_title, article_date, article_source, article_content)) db.commit() except Exception as e: db.rollback() print(e) # 关闭数据库连接 db.close()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.