写一段python代码搜索21世纪经济报道,澎湃、财经、财新、华夏时报、每日经济新闻、新京报中股票利空和利好消息
- User
写一段python代码搜索21世纪经济报道,澎湃、财经、财新、华夏时报、每日经济新闻、新京报中股票利空和利好消息
- Assistant
import requests from bs4 import BeautifulSoup
设置搜索关键词,这里我们搜索股票利空和利好消息
keywords = ['股票利空', '股票利好']
设置需要搜索的网站
sites = ['http://www.21jingji.com', 'https://www.thepaper.cn', 'https://finance.sina.com.cn', 'https://www.hxnews.com', 'https://www.nbd.com.cn', 'https://www.bjnews.com.cn']
定义一个函数用于搜索关键词
def search(keyword, site): url = site + '/search/result.jspa?q=' + keyword html = requests.get(url).content soup = BeautifulSoup(html, 'html.parser') results = soup.find_all('div', {'class': 'search-item'}) for result in results: title = result.find('h3').get_text() link = result.find('a')['href'] print(title) print(link) print()
循环搜索每个网站和关键词
for keyword in keywords: for site in sites: search(keyword, site)
- User
把编码转换问题考虑进去,重新写
- Assistant
import requests from bs4 import BeautifulSoup
设置搜索关键词,这里我们搜索股票利空和利好消息
keywords = ['股票利空', '股票利好']
设置需要搜索的网站
sites = ['http://www.21jingji.com', 'https://www.thepaper.cn', 'https://finance.sina.com.cn', 'https://www.hxnews.com', 'https://www.nbd.com.cn', 'https://www.bjnews.com.cn']
定义一个函数用于搜索关键词
def search(keyword, site): url = site + '/search/result.jspa?q=' + keyword r = requests.get(url) r.encoding = r.apparent_encoding # 修正编码 html = r.content soup = BeautifulSoup(html, 'html.parser') results = soup.find_all('div', {'class': 'search-item'}) for result in results: # 对标题和链接进行编码转换 title = result.find('h3').get_text() link = result.find('a')['href'] title = title.encode('gbk', 'ignore').decode('gbk', 'ignore') link = link.encode('gbk', 'ignore').decode('gbk', 'ignore') print(title) print(link) print()
- User
把缩进排版考虑进去,重新写
- Assistant
import requests from bs4 import BeautifulSoup
设置搜索关键词,这里我们搜索股票利空和利好消息
keywords = ['股票利空', '股票利好']
设置需要搜索的网站
sites = ['http://www.21jingji.com', 'https://www.thepaper.cn', 'https://finance.sina.com.cn', 'https://www.hxnews.com', 'https://www.nbd.com.cn', 'https://www.bjnews.com.cn']
定义一个函数用于搜索关键词
def search(keyword, site): url = site + '/search/result.jspa?q=' + keyword r = requests.get(url) r.encoding = r.apparent_encoding # 修正编码 html = r.content soup = BeautifulSoup(html, 'html.parser') results = soup.find_all('div', {'class': 'search-item'}) for result in results: # 对标题和链接进行编码转换 title = result.find('h3').get_text() link = result.find('a')['href'] title = title.encode('gbk', 'ignore').decode('gbk', 'ignore') link = link.encode('gbk', 'ignore').decode('gbk', 'ignore') print(title) print(link) print()
- User
写一段python代码将21世纪经济报道,澎湃、财经、财新、华夏时报、每日经济新闻、新京报各网站中和股票相关的消息标题显示出来,显示格式为股票代码,股票名称,消息标题
- Assistant
import requests from bs4 import BeautifulSoup
设置需要搜索的网站
sites = ['http://www.21jingji.com', 'https://www.thepaper.cn', 'https://finance.sina.com.cn', 'https://www.hxnews.com', 'https://www.nbd.com.cn', 'https://www.bjnews.com.cn']
定义一个函数用于搜索股票相关消息标题
def search_stock_news(site): url = site + '/stock/' r = requests.get(url) r.encoding = r.apparent_encoding # 修正编码 html = r.content soup = BeautifulSoup(html, 'html.parser') results = soup.find_all('a', {'target': '_blank'}) for result in results: title = result.get_text().strip() link = result['href'] if ('stock/game/' in link or 'stock/comment/' in link or 'stock/markets/' in link or 'news.sina.com.cn' in link or '' in link or 'wap.thepaper.cn' in link): continue if ('www' not in link and 'finance' not in link and 'money' not in link and 'stock' not in link and 'news' not in link): continue if 'http' not in link: if link[0] == '/': link = site + link else: link = site + '/' + link print(link.split('/')[3], link.split('/')[4], title)