python3爬取微博图片的代码
时间: 2024-06-11 21:01:28 浏览: 14
当然可以!以下是一个简单的Python3代码示例,用于爬取微博图片:
```python
import requests
from bs4 import BeautifulSoup
# 定义要爬取的微博页面链接
url = 'https://weibo.com/xxxxxx'
# 发送HTTP请求,获取页面内容
response = requests.get(url)
html = response.text
# 使用BeautifulSoup解析页面内容
soup = BeautifulSoup(html, 'html.parser')
# 找到所有的图片标签
img_tags = soup.find_all('img')
# 遍历图片标签,获取图片链接并下载
for img_tag in img_tags:
img_url = img_tag['src']
# 发送HTTP请求,下载图片
img_data = requests.get(img_url).content
# 保存图片到本地
with open('image.jpg', 'wb') as f:
f.write(img_data)
print('图片下载成功!')
```
请注意,上述代码仅供参考,实际使用时需要根据具体情况进行修改。另外,爬取网站内容需要遵守相关法律法规和网站的使用规定,确保合法合规。
相关问题
python爬取微博关键词代码
以下是Python爬取微博关键词的代码示例:
```python
import requests
from bs4 import BeautifulSoup
import jieba
from wordcloud import WordCloud
# 设置请求头
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
# 获取搜索结果页面
def get_page(keyword, page):
url = 'https://s.weibo.com/weibo?q=' + keyword + '&page=' + str(page)
response = requests.get(url, headers=headers)
return response.text
# 解析搜索结果页面,获取微博内容
def get_content(html):
soup = BeautifulSoup(html, 'html.parser')
content_list = soup.find_all('p', class_='txt')
content = ''
for item in content_list:
content += item.text.strip()
return content
# 对微博内容进行分词
def cut_words(content):
word_list = jieba.cut(content)
words = ' '.join(word_list)
return words
# 生成词云图片
def generate_wordcloud(words):
wc = WordCloud(background_color='white', width=800, height=600, max_words=200, font_path='msyh.ttc')
wc.generate(words)
wc.to_file('wordcloud.png')
# 主函数
def main():
keyword = input('请输入要搜索的关键词:')
page = int(input('请输入要搜索的页数:'))
content = ''
for i in range(1, page+1):
html = get_page(keyword, i)
content += get_content(html)
words = cut_words(content)
generate_wordcloud(words)
if __name__ == '__main__':
main()
```
以上代码可以实现输入关键词和页数,然后爬取对应页数的微博内容,对内容进行分词并生成词云图片。需要注意的是,爬取微博数据需要登录账号并获取cookie,否则会被反爬虫机制拦截。
python爬虫爬取微博相册
爬取微博相册的Python爬虫可以通过以下步骤实现:
1. 导入所需的库。
2. 初始化队列,用于存储待下载的图片链接。
3. 循环获取每一页的相册数据。
4. 解析数据,获取每张图片的链接。
5. 将图片链接添加到队列中。
6. 开启多线程,从队列中取出链接并下载对应的图片。
以下是示例代码:
```python
import requests
import random
from queue import Queue
from threading import Thread
# 初始化队列
data_queue = Queue()
# 循环获取每一页的相册数据
for i in range(1, 26):
params = {
'uid': '1739046981',
'album_id': '3581934839144367',
'count': '30',
'page': i,
'type': '3',
'__rnd': '1609667183074'
}
response = requests.get(url=url, params=params, headers=headers).json()
# 解析数据,获取每张图片的链接
photo_list = response.get('data').get('photo_list')
for photo in photo_list:
pic_name = photo.get('pic_name')
photo_url = 'https://wx4.sinaimg.cn/large/' + pic_name
# 将图片链接添加到队列中
data_queue.put({photo_id: photo_url})
# 定义爬取图片的线程类
class SpiderImg(Thread):
def __init__(self, data):
Thread.__init__(self)
self.data = data
def run(self):
# 当队列不为空的时候下载对应的图片
while not self.data.empty():
# 取队列
a = self.data.get()
# 获得id和url数据
i_id = list(a.keys())[0]
i_url = list(a.values())[0]
# 下载图片
img = requests.get(url=i_url, headers=fake, proxies=random.choice(proxy_pool)).content
# 将文件写入指定路径下的文件夹
with open('E:/SpiderImg/李荣浩/{}.jpg'.format(i_id), 'wb') as t:
t.write(img)
print(i_id + '.jpg' + ' ' * 4 + '下载完毕' + '.' * 4)
# 开启多线程,根据队列中存放的url数据,下载图片
for w in range(64):
spider = SpiderImg(data_queue)
spider.start()
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)