import requests # import re from bs4 import BeautifulSoup from openpyxl import Workbook import time wb = Workbook() sheet = wb.active count = 1 # 拼接电影爬去地址url for line in range(0, 100, 25): url = f'https://movie.douban.com/top250?start={line}&filter=' # 向对方服务器发送请求,获取响应数据 response = requests.get(url) # 拿到文本数据 data = response.text # 通过'html.parser'解析器解析数据 soup = BeautifulSoup(data, 'html.parser') # 找到类名为:grid_view的ol标签 ol = soup.find(name="ol", attrs={'class': 'grid_view'}) # 拿到中间的25个li标签,存入列表中 li_list = ol.find_all(name='li') sheet.title = '好评电影' sheet['A1'].value = '序号' sheet['B1'].value = '电影名称' sheet['C1'].value = '电影评分' sheet['D1'].value = '电影链接' sheet['E1'].value = '电影图片' for li in li_list: # 找到类名为:title的span标签 name = li.find(name='span', attrs={'class': 'title'}) url = li.find(name='a') rat = li.find(name='span', attrs={'class': 'rating_num'}) img = li.find(name='img') count = count + 1 sheet['A%s' % (count)].value = count - 1 sheet['B%s' % (count)].value = name.text sheet['C%s' % (count)].value = rat.text sheet['D%s' % (count)].value = url['href'] sheet['E%s' % (count)].value = img['src'] time.sleep(1) wb.save('好评电影.xlsx') import requests import json def main(key=""): header = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36' } url = 'http://fy.iciba.com/ajax.php?a=fy' data = { 'f': 'auto', 't': 'auto', 'w': key } # 向对方服务器发送post请求,带上headers,数据data res = requests.post(url=url, headers=header, data=data) data = res.text # 将二进制数据反序列化 data_list = json.loads(data) # print(data_list) try: val = data_list['content']['word_mean'] except: val = data_list['content']['out'] return val if __name__ == '__main__': # 接收用户输入 key = input('请输入要翻译的词语:').strip() # 判断如果输入为空将要执行的代码 if not key: print('输入为空') else: data = main(key=key) print(data)