爬虫代码如下:
import requests
from bs4 import BeautifulSoup
import os
import re
#1、设置爬取网站以及网页请求
url="https://image.so.com/i?ie=utf-8&src=hao_360so&q=python#/"
response=requests.get(url) #对网站进行get请求并赋值给response
response.encoding="utf-8" #设置网页编码为utf-8
html=response.text
#2、对网站源代码进行解析
soup=BeautifulSoup(html,'lxml')
print(soup.body) #打印网页源代码的body标签内容
print(soup.div) #打印网页源代码的div标签内容
print(soup.ul) #打印网页源代码的ul标签内容
#3、查找符合要求的内容
print(soup.findAll("a")) #打印网站所有的a标签信息
for text1 in soup.findAll("a"): #遍历网页源代码的a标签信息
print(text1.text) #打印图片的名称信息运行结果如下图所示:
来源:厚积薄发爱python
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4662/viewspace-3155481/ ,如需转载,请注明出处,否则将追究法律责任。