Python爬58同城二手房图片
from bs4 import BeautifulSoup
import requests
import os
import urllib.request
import random
import time
import re
user_agent = ['Mozilla/5.0 (Windows NT 6.1)\
AppleWebKit/537.11 (KHTML, like Gecko)\
Chrome/23.0.1271.64 Safari/537.11','Mozilla/5.0 (Windows NT 6.1; WOW64)\
AppleWebKit/537.36 (KHTML, like Gecko)\
Chrome/47.0.2526.106 Safari/537.36','Mozilla/5.0 \
(Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0',"Mozilla/5.0\
(X11; Linux x86_64) AppleWebKit/537.17 (KHTML, like Gecko)\
Chrome/24.0.1312.56 Safari/537.17",'Mozilla/5.0\
(Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0']
url=[]
for i in range(1000):
if i==0:
url.append('http://gz.58.com/ershoufang/')
else:
url.append('http://gz.58.com/ershoufang/pn{0}/'.format(i))
print("url is done!")
b=0
url=['http://gz.58.com/ershoufang/']
cd week7
cd douban
for i in url:
time.sleep(1)
agent = random.choice(user_agent)
header= {
'Connection': 'Keep-Alive',
'Accept': 'text/html, application/xhtml+xml, */*',
'Accept-Language': 'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3',
'User-Agent': '%s' %agent}
soup=BeautifulSoup(requests.get(i,headers = header).text,"html.parser")
items=soup('tr',logr=re.compile('^j'))
if len(items)==0:
break
else:
for item in items:
urllib.request.urlretrieve(item.find('div','img_list').img.get('lazy_src'),
os.path.basename(item.find('p','bthead').a.get_text()+'.jpg'))
b+=1
print("下载%d张"%int(b))
print("Finish Down %d Picture" %int(b))