http .get("http://quan.suning.com/getSysTime.do") .then((res) => { console.log(res); }) .catch((err) => { err; });
时间: 2024-02-14 09:27:37 浏览: 151
根据你提供的代码,你使用 `http` 实例的 `get` 方法发送了一个请求到 "http://quan.suning.com/getSysTime.do" 的 URL,然后通过 `.then` 方法处理请求的响应。在 `.then` 方法中,你将响应结果打印到控制台。
另外,你还使用 `.catch` 方法来处理可能发生的错误,但是当前的 `.catch` 方法并没有对错误进行任何处理,只是简单地返回了错误本身。你可以在 `.catch` 方法中添加适当的错误处理逻辑,比如打印错误信息或执行其他操作。
请确保已经正确导入了 `http` 模块,并且在调用 `get` 方法时返回了一个 `Promise` 对象,以便能够使用 `.then` 和 `.catch` 方法。如果你遇到了其他问题,请提供更多的代码细节,以便我可以更好地帮助你。
相关问题
for i in range(1, 6): # 循环遍历 1-5 页 url = f'https://www.haochu.com/caipu/134?page={i}' # 好厨————川菜类菜品网页url response = requests.get(url=url, headers=headers) html_data = etree.HTML(response.text) # 获取川菜类菜品网址的html_data dish_names = html_data.xpath('//div[@class="bei"]/span[1]/text()') # 菜名 dish_heats = html_data.xpath('//div[@class="bei"]/span[2]/text()') # 菜人气度 dish_urls = html_data.xpath('//div[@class="quan_r"]/ul/li/a/@href') # 菜品具体网址 dish_imgs = html_data.xpath('//div[@class="quan_r"]/ul/li/a/img/@src') # 菜品图片 with open('./第{}页.txt'.format(i), 'w', encoding='utf-8') as fp: for dish_name, dish_heat, dish_url in zip(dish_names, dish_heats, dish_urls): # 获取每道菜品的具体网址所需的食材信息 dish_url = f'https://www.haochu.com{dish_url}' response2 = requests.get(url=dish_url, headers=headers) html_data2 = etree.HTML(response2.text) dish_ints_names = html_data2.xpath('//div[@class="ints-cover"]/img/@alt') dish_imgs_url = html_data2.xpath('//div[@class="caipu-detail-cover"]/img/@src') filepath = f'{dish_name}.jpg' response3 = requests.get(url='https:' + ''.join(dish_imgs_url)) for imags in response3: with open(filepath, "wb") as f: f.write(response3.content) # 将菜品名、热量、所需食材、具体网址等信息写入到文本文件中 dish = f' 菜名:{dish_name} 人气度:{dish_heat} 食材:{dish_ints_names} {dish_url}\n' fp.write(dish),我要将图片保存在当前目录的另一个文件夹里
好的,您可以在代码中添加以下内容来保存图片到指定文件夹:
```python
import os
# 创建保存图片的文件夹
if not os.path.exists('./images'):
os.makedirs('./images')
with open('./第{}页.txt'.format(i), 'w', encoding='utf-8') as fp:
for dish_name, dish_heat, dish_url, dish_imgs_url in zip(dish_names, dish_heats, dish_urls, dish_imgs):
# 获取每道菜品的具体网址所需的食材信息
dish_url = f'https://www.haochu.com{dish_url}'
response2 = requests.get(url=dish_url, headers=headers)
html_data2 = etree.HTML(response2.text)
dish_ints_names = html_data2.xpath('//div[@class="ints-cover"]/img/@alt')
dish_imgs_url = html_data2.xpath('//div[@class="caipu-detail-cover"]/img/@src')
# 保存菜品图片到指定文件夹
filepath = f'./images/{dish_name}.jpg'
response3 = requests.get(url='https:' + ''.join(dish_imgs_url))
with open(filepath, "wb") as f:
f.write(response3.content)
# 将菜品名、热量、所需食材、具体网址等信息写入到文本文件中
dish = f' 菜名:{dish_name} 人气度:{dish_heat} 食材:{dish_ints_names} {dish_url}\n'
fp.write(dish)
```
这样修改后,菜品图片会保存在当前目录下的 `images` 文件夹中。
with open('./第{}页.txt'.format(i), 'w', encoding='utf-8') as fp: for num in dish_urls: new_url = f'https://www.haochu.com{num}' # 把菜品具体网址作为新url,获取每道菜所需的食材 print(new_url) response2 = requests.get(url=new_url, headers=headers) html_data2 = etree.HTML(response2.text) # 新网址的html——data2 ints_names = html_data2.xpath('//div[@class="ints-cover"]/img/@alt') # 菜品所需食材 print(ints_names) for dish_name, dish_heat, dish_url in zip(dish_names, dish_heats, dish_urls): # 获取每道菜品的具体网址所需的食材信息 dish_url = f'https://www.haochu.com{dish_url}' response3 = requests.get(url=dish_url, headers=headers) html_data3 = etree.HTML(response3.text) dish_ints_names = html_data3.xpath('//div[@class="ints-cover"]/img/@alt') # 将菜品名、热量、所需食材、具体网址等信息写入到文本文件中 dish = f'{dish_name},{dish_heat},{dish_ints_names},https://www.haochu.com{dish_url}\n' fp.write(dish),我想将 dish_imgs = html_data.xpath('//div[@class="quan_r"]/ul/li/a/img/@src')#菜品图片也加进去
可以将获取的菜品图片链接加入到 `dish` 变量中,最后写入文本文件。修改后的代码如下:
```
with open('./第{}页.txt'.format(i), 'w', encoding='utf-8') as fp:
for num in dish_urls:
new_url = f'https://www.haochu.com{num}'
# 把菜品具体网址作为新url,获取每道菜所需的食材
print(new_url)
response2 = requests.get(url=new_url, headers=headers)
html_data2 = etree.HTML(response2.text)
# 新网址的html——data2
ints_names = html_data2.xpath('//div[@class="ints-cover"]/img/@alt') # 菜品所需食材
dish_imgs = html_data2.xpath('//div[@class="cover-img"]/img/@src') # 菜品图片
print(ints_names)
for dish_name, dish_heat, dish_url, dish_img in zip(dish_names, dish_heats, dish_urls, dish_imgs):
# 获取每道菜品的具体网址所需的食材信息
dish_url = f'https://www.haochu.com{dish_url}'
response3 = requests.get(url=dish_url, headers=headers)
html_data3 = etree.HTML(response3.text)
dish_ints_names = html_data3.xpath('//div[@class="ints-cover"]/img/@alt')
# 将菜品名、热量、所需食材、具体网址、菜品图片等信息写入到文本文件中
dish = f'{dish_name},{dish_heat},{dish_ints_names},https://www.haochu.com{dish_url},{dish_img}\n'
fp.write(dish)
```
阅读全文