使用python写一个爬取百度每日天气程序
时间: 2023-02-21 10:47:22 浏览: 72
可以尝试使用Python的requests库和BeautifulSoup库来编写一个爬取百度每日天气的程序。可以利用requests库来发送HTTP请求,获取百度天气页面的HTML代码,然后使用BeautifulSoup库来解析HTML代码,从中提取出每日天气的信息。
相关问题
使用python写一个爬取百度图片的爬虫
好的,以下是使用Python编写爬取百度图片的爬虫的代码:
```python
import requests
import re
def get_img_urls(keyword, num):
imgs = []
for i in range(num):
url = f'https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&word={keyword}&pn={i * 30}&rn=30'
response = requests.get(url)
img_urls = re.findall(r'"thumbURL":"(.*?)",', response.text)
imgs += img_urls
return imgs
def download_imgs(keyword, num):
img_urls = get_img_urls(keyword, num)
for i, url in enumerate(img_urls):
response = requests.get(url)
with open(f'{keyword}_{i}.jpg', 'wb') as f:
f.write(response.content)
print(f'Saved {keyword}_{i}.jpg')
if __name__ == '__main__':
download_imgs('猫', 10)
```
这个爬虫可以通过搜索关键词来爬取百度图片,并下载到本地。
使用Python写一个爬取百度图片图像的爬虫
以下是一个使用Python编写的爬取百度图片的爬虫代码:
```python
import requests
import os
def download_images(url, save_folder):
response = requests.get(url)
if response.status_code == 200:
image_name = url.split("/")[-1]
image_path = os.path.join(save_folder, image_name)
with open(image_path, "wb") as f:
f.write(response.content)
print(f"{image_name} 下载成功!")
else:
print(f"{url} 下载失败!")
def crawl_images(keyword, save_folder):
if not os.path.exists(save_folder):
os.makedirs(save_folder)
url = "https://image.baidu.com/search/index"
params = {
"tn": "baiduimage",
"word": keyword,
"pn": 0,
"rn": 30,
"ie": "utf-8",
"ic": 0,
"s": "1",
"se": "1",
"tab": "0",
"width": "",
"height": "",
"face": "",
"istype": "",
"qc": "",
"nc": "1",
"fr": "",
"pn": "0"
}
response = requests.get(url, params=params)
if response.status_code == 200:
html = response.text
image_urls = [url.replace("\\", "") for url in html.split('"') if url.startswith("http") and url.endswith(".jpg")]
for index, image_url in enumerate(image_urls):
download_images(image_url, save_folder)
else:
print(f"爬取图片失败!错误代码:{response.status_code}")
if __name__ == "__main__":
keyword = input("请输入要搜索的图片关键词:")
save_folder = input("请输入要保存的文件夹路径:")
crawl_images(keyword, save_folder)
```
使用方法:
1. 运行代码,输入要搜索的图片关键词和要保存的文件夹路径。
2. 程序会向百度图片发送请求,并获取相应的图片链接。
3. 程序会按照链接依次下载图片,并保存到指定的文件夹路径中。
注意事项:
1. 本程序只支持下载jpg格式的图片。
2. 爬取百度图片涉及到版权问题,请在使用时遵守相关法律法规。