import requests
from bs4 import BeautifulSoup
import re
def search001(begin, over,keyword = '关键词'):
page_num = range(begin,over)
res_list = []
# 搜索n页内容
for i in page_num:
# get代码
url = 'http://地址/forum/forum-187-' + str(i) + '.html'
req = requests.get(url)
req.encoding = 'utf-8'
soup = BeautifulSoup(req.text, 'lxml')
# get帖子信息
soup_find = soup.find_all('span', id=re.compile('thread_\d+'))
# 搜索,存储结果
for i in soup_find:
str_i = str(i)
if keyword in str_i:
res_list.append(str_i)
else:
continue
# 后处理
for j in res_list:
res_url = re.search(r'thread-\d+-\d+-\d+.html', j).group(0)
res_title = re.search(r'<span.*>(.*)</a>', j).group(1)
full_url = 'http://地址/forum/' + res_url
print('标题:%s\n链接:%s '%(res_title,full_url))
return 'Done!'
def search_again():
count = 100
begin = 101
over = 111
while count != 0:
search001(begin,over,keyword = 'XN')
begin += 10
over += 10
count -=1
print('【%d】-【%d】ok!'%(begin,over))
search_again()