同学项目需要大量交通标志,这里就实现一个爬取某交通网站的交通标志图片功能:
import urllib2
import urllib
from bs4 import BeautifulSoup
def get_html(url):#获取到网页的基础html
request=urllib2.Request(url)
request.add_header('User-Agent',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299')
html_content=urllib2.urlopen(request).read()
return html_content
if __name__=='__main__':
html = get_html('http://www.jsyks.com/jtbz_1112')#想要爬取的网址
soup = BeautifulSoup(html, 'html.parser')
x = 1
for img in soup.find_all('img'):
if img.has_attr('src') and img['src'].startswith('//sucimg.itc.cn/sblog/o'):
image_url = img['src']
image_url='http:'+image_url
print(image_url)
# os.mkdir("pic1")
# os.chdir("pic1")
# with open((str(x)+".jpg"),"wb") as f: